EJB 2.0 specs says: If bean using Bean Managed Transaction (BMT) demarcation throws application exception, container re-throws application exception.
However, specification does not mention about what happens to transaction associated with the Bean. If bean instance called UserTransaction.setRollbackOnly() in method before throwing application exception, one needs to rollback transaction or commit it, isn't it? So who's responsible to do the same? Is it not Container? I think it's Container who co-ordinates with Transaction Manager. But if it's not the Container, please correct me and tell who does commit or rollback in the above mentioned case.
Regards,<br />Sandesh<br />(SCJCP, SCWCD, SCBCD - 99%, OCP-1)<br /> <br />Either find a way or create one.
It depends on what type of bean you are looking at. For stateless session beans using BMT the spec says the following.
----- If a stateless session bean instance starts a transaction in a business method, it must commit the transaction before the business method returns. The Container must detect the case in which a transaction was started, but not completed, in the business method, and handle it as follows: - Log this as an application error to alert the system administrator. - Roll back the started transaction. - Discard the instance of the session bean. - Throw the java.rmi.RemoteException to the client if the client is a remote client, or throw the javax.ejb.EJBException if the client is a local client. -----
The same applies for message driven beans, except for the last point because there is no client.
For stateful session beans the container can retain the transaction across multiple client calls and will therefore not rollback the transaction.
Hi, well.. my understanding is, if you are using BMT, then it's upto the bean provider to do all the transaction stuffs. So in this case .. the bean provider has to check the tx status usign UserTx.getStatus() and then do a commit() or rollback() based on it. So there's nothing the container can do.
SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCEA
Joined: Jun 22, 2003
So sorry guys. I was bit lost in bean with CMT & BMT. :roll:
Whether to commit or rollback transaction is sole responsibility of the bean provider in case of bean using BMT and NOT the container.
I take back my question. Whatever specs says is correct. The good part is my understanding has become crystal clear now. Hope this confusion would help others also.
I got confused by Christian and Sandesh. Based on what posted by Christian, the container will Rollback the Transcation even it's BMT bean, but later Sandesh said if this is BMT bean, then Bean provider to roll back? Isn't it conflicting? [ July 07, 2004: Message edited by: Hai Lin ]
Joined: Jun 22, 2003
What Christian is pointing out is a case where bean does not end transaction started before completing the method. Remember, only Stateful Session bean can keep transaction open across multiple method calls and NOT Stateless Session Bean or Message-driven Bean can do it.
If bean instance uses BMT and throws Application Exception, before throwing an Application exception bean can either rollback or commit transaction by calling UserTransaction.commit() or UserTransaction.rollback(). Note here bean will get an opportunity to rollback or commit transaction.
If bean instance uses BMT and throws System Exception, Container marks for rollback a transaction that has been started, but not yet completed, by the bean instance. Note here bean MAY NOT get an opportunity to rollback or commit transaction.
So I need to revise my statment as below: "Whether to commit or rollback transaction is responsibility of bean provider in case of bean using BMT throws Application exception and container rollbacks transaction when bean throws System Exception."
I hope this makes things very clear.
subject: Application Exception thrown by Bean with BMT