File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes EJB Transaction Management Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB Transaction Management" Watch "EJB Transaction Management" New topic

EJB Transaction Management

Arockia Raj
Ranch Hand

Joined: Dec 08, 2006
Posts: 37
Hi All,
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

francesco umani

Joined: Aug 07, 2013
Posts: 27
Transaction is rolled back in case you throw a RuntimeException or any Exception which have @ApplicationException annotation with rollback attribute set to true, so:

public class MyException extends Exception {
// ...

will rollback the current transaction.

By default ApplicationException doesn't rollback your transaction.

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).
Arockia Raj
Ranch Hand

Joined: Dec 08, 2006
Posts: 37
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..
francesco umani

Joined: Aug 07, 2013
Posts: 27
Maybe with the REQUIRES_NEW is only rolled back the new transaction created?
Dieter Quickfend

Joined: Aug 06, 2010
Posts: 504

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.

Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
jQuery in Action, 2nd edition
subject: EJB Transaction Management
Similar Threads
Stateless Session Beans Managing JDBC Transactions
question about EJB transaction
Few Questions
Can i rollback a transaction in EJB2.0 by thorowing an EJBException explicitly?
Txn not automatically rolling back after RemoteException