EJB programming & troubleshooting: Transaction in CMP Entity Bean
If I am to update serveral table in the DB. I will need to update serveral entity beans. The questions here is:
- Posted by: Jimmy Tong
- Posted on: April 11 2002 14:07 EDT
i) How to I make sure that all entity bean share the same transaction? Is it taken care by the container? Or I need to implement JTA API?
ii) What about BMP?
1) Remember that session beans have transactions too. So most likely you will have a session bean, which calls several different entity beans to make db changes. When you do a rollback in the session bean it can roll back all of the entiy beans. So this is no problem.
2) I'm less clear on BMP. At a guess you can still use the DB to do a rollback, but I havn't tried it out so I'm not sure.
Does it means that it is advisable to implement a Session bean facade to encapsulate the entity bean? Am I rite to say that if I update serveral tables in a session bean method say updateRecord(), if half way thru the updating, an sqlException is thrown, all the record and action will be rollback?
If I don't want to use a session facade, is there a way out?
Thanks for the advice.
If you use a Session Bean with the right transaction attributes, then all the call to different entity beans (assuming their transaction attributes are set properly) will be atomic (all or nothing). If you don't want to use a Session Bean facade, then you would need to use the javax.jta.UserTransaction API to demarcate your transaction.
i) How to I make sure that all entity bean share the same
> transaction? Is it taken care by the container? Or I need
> to implement JTA API?
This completely depends on the context from which you invoke the EJBs that need to share the transaction. If, as Daniel points out, you have a Session Bean method that makes the calls to the different Entity Beans and that Session Bean method is being invoked in a transactional context, then it will happen automatically as long as you make sure that the transactional attribute of all the beans are set properly.
> ii) What about BMP?
BMP is completely orthogonal to container-managed transactions. As long as you have correctly configured your BMP beans to get their database connections from a JTA aware mechanism (e.g., in WebLogic, you just set up a TxDataSource in the Server Console and have your entity beans look up and use this DataSource in their code), then both BMP and CMP beans can participate in container-managed transactions.
Hope this helps,