This is regarding EJB container behaviour during transaction management..
I am having EJB AccountService calling method on AccountDAO saveAccountInfo as shown below.Both of these exceptions are RuntimeExceptions(extented from RunTimeException Class).We will call this method in look in AccountService and expected everytime we call this method transaction will get comitted or rollbacked based on the outcome of the method.For some conditions this methods throws RDMApplicationException for which the transactions are getting comitted.We expected the rollback if the method throws Runtime exceptions.Can Someone please clarify this..
public void saveAccountInfo(AccountInfo AccountInfo) throws RDMApplicationException,RDMSystemException
If you don't want to methodB to rollback your transaction you can either change the rollback behavior of your ApplicationException or prevent the transaction sharing.
The latter is achievable by changing the TransactionAttribute of methodB i.e. to RequiresNew. Then methodA transaction (Tx1) will be suspendend and in case methodB throws an exception which results in rollback of its transaction (Tx2), you can still catch it in methodA and prevent rollback of your methodA transaction (Tx1).
Joined: Dec 08, 2006
Thanks for the post...our problem is for some scenarios method B is throwing RDMSystemException which is a Runtime Exception i.e RDMSystemException extends from java.lang.RuntimeException.When the method B whose transaction attribute is REQUIRES_NEW throws RDMSystemException which is runtime exception,it should rollback the transaction but we are seeing the transaction is getting comitted..
Joined: Aug 07, 2013
Maybe with the REQUIRES_NEW is only rolled back the new transaction created?
Your transaction attribute is REQUIRES_NEW, so when this method throws a system exception, that transaction (with REQUIRES_NEW) will be rolled back. The surrounding transaction, if there is one, however, will receive EJBException, and can choose whether or not to continue its own transaction.
If the transaction attribute would be REQUIRED, and the client is an EJB in the same transaction, it will receive EJBTransactionRolledBackException and, if necessary, realize that continuing the transaction is useless.
But the inner transaction (with REQUIRES_NEW) as in your example, should always be rolled back according to the EJB specification, if it throws a SystemException that is NOT marked with @ApplicationException.
When EJBException is thrown to the client (methodA), this transaction may or may not be rolled back (depending on the vendor).
For more information, check out EJB 3.1 spec, page 386, table 15.