File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJB 3, Transaction question 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 "EJB 3, Transaction question" Watch "EJB 3, Transaction question" New topic
Author

EJB 3, Transaction question

Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
This is a very concise and short question just to clarify one concept.

What happend if in the middle of a EJB transaction-REQUIRED method, a RequiresNew-method in another EJB is called, and this RequiresNew-method happens to throw a System Exception ? ... and what happend if it throws an application exception ? .. I mean, what happend with the transaction of the first calling method ?, if it rolls back, as i think it should ,,, whats the difference, from the developers view, between a REQUIRED-method and a RequiresNew-Method ? ... I know the theory ... it must start a new transaction ... but ... whats the difference if it do not start a new one, and just is included in the transaction scope of the calling method ?

Hope my english is not too bad,,,

Thanks for your help,

Camilo Morales,
SCJP 5


Camilo Morales<br />SCJP 5, SCBCD 5
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380

What happend if in the middle of a EJB transaction-REQUIRED method, a RequiresNew-method in another EJB is called, and this RequiresNew-method happens to throw a System Exception ?


B1 - with Tx1 calls B2 with Tx2 (new). And then B2 throws System Exception.
Since the Container manages the System Exception it will :

1. Log the exception
2. Rollback Tx2
3. Discard the instance of B2
and throw EJBException to B1 with Tx1.

Now it is upto B1 with Tx1 how to handle the EJBException. If it doesn't catch and ignore it then an EJBException being raised from B1 would result in the same sequence of steps taken by container as above. i.e Tx1 would also rollback.


.. and what happend if it throws an application exception ? ..


For Application Exception - if the exception is marked with the flag rollback=true or if B2 marked the Tx2 for rollback using setRollbackOnly then the container rollback the Tx2 and forward the application exception.

It is upto the Bean B1 to either eat up the application exception and continue to work in Tx1 or forward it back to the calling client i.e rethrow Application Exception - In this case - the behavior of the container would be same as worked for B2/Tx2.


See - if the Tx is same for bean B1 and B2 - then the container starts to take action in rollback/commit in the bean which actually started the Tx. Otherwise container propagates exception (and/or) logs exptn,discards the instance only.

But if Tx is different for both beans then actions are taken per bean where Tx started and then Exception propagated to the caller.

The one who starts the Tx has the control - that is the difference...

Regards,
Shivani


/** Code speaks louder than words */
Camilo Morales
Ranch Hand

Joined: Jul 14, 2007
Posts: 92
Thanks
 
Don't get me started about those stupid light bulbs.
 
subject: EJB 3, Transaction question
 
Similar Threads
Message-driven beans and transaction attributes
Questions
HF page 500 for MDB.....
Transaction Question from spec
Stateless beans and SessionSynchronization