The EJB specification says that we cannot use Bean Managed Transaction in Entity Beans. Why?

Matt Goodall

The short, practical answer is... because it makes your entity beans useless as a reusable component. Also, transaction management is best left to the application server - that's what they're there for.

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:
  1. Use the javax.transaction package
  2. Use a session bean to represent usecases and specify how transactions should be managed as part of the session bean's deployment descriptor.
Solution 2 is the "correct" way to do it. Either way, you can simply leave all transaction code out of your entity beans.

Any good EJB book will explain more about this.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact