The EJB specification says that we cannot use Bean Managed Transaction in Entity Beans. Why?
Created May 7, 2012
It's all about atomic operations on your data. If an operation updates more than one entity then you want the whole thing to succeed or the whole thing to fail, nothing in between. If you put commits in the entity beans then it's very difficult to rollback if an error occurs at some point late in the operation.
Think of an account transfer which takes money from the first account and then pays it into a second account. If the paying in part fails how do you put the money back in the first account? What about if that then fails too?
The transaction should always "wrap" the entire operation. There are two obvious ways of achieving this in an EJB environment:
- Use the javax.transaction package
- Use a session bean to represent usecases and specify how transactions should be managed as part of the session bean's deployment descriptor.
Any good EJB book will explain more about this.