Why bean managed transaction is not allowed with entity beans
posted 11 years ago
I know that many people want to know the exact answer to the question "why BMT is not allowed with entity beans?". The answer was made clear to me after I reviewed Mastering EJB 3rd Edition by Ed Roman (pg 315). In a nutshell it is like this: in order to start a transaction the container calls ejbLoad() to acquire the locking and get the last copy of data from the database. Finally the container calls ejbStore() in order to update the database and commit the transaction. The point is that allowing the bean to start the transaction then this should happen inside of ejbLoad() (before acquiring the db lock) and it should be committed inside of ejbStore() (before releasing the lock). This implies that the container will call ejbLoad() and ejbStore() only once and exactly in this order. This actually doesn’t happen all the time and is possible that ejbStore() doesn’t get called at the end of the transaction (some container choose to cache the data between transactions). In this case one might get in a terrible situation when the transaction gets never committed.