aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Application Exception thrown by Bean with BMT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Application Exception thrown by Bean with BMT" Watch "Application Exception thrown by Bean with BMT" New topic
Author

Application Exception thrown by Bean with BMT

Sandesh Tathare
Ranch Hand

Joined: Jun 22, 2003
Posts: 82
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,
Sandesh


Regards,<br />Sandesh<br />(SCJCP, SCWCD, SCBCD - 99%, OCP-1)<br /> <br />Either find a way or create one.
Christian D. Th. Sellberg
Greenhorn

Joined: Apr 02, 2004
Posts: 10
Hi Sandesh,

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.

Kind regards
Christian
Giju George
Ranch Hand

Joined: Jun 08, 2004
Posts: 333
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
Sandesh Tathare
Ranch Hand

Joined: Jun 22, 2003
Posts: 82
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.

Regards,
Sandesh
Hai Lin
Ranch Hand

Joined: May 23, 2004
Posts: 79
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 ]
Sandesh Tathare
Ranch Hand

Joined: Jun 22, 2003
Posts: 82
Hai,

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.

Regards,
Sandesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Application Exception thrown by Bean with BMT