Accueil‎ > ‎

EJB - Unexpected Transaction Rollback

posted 11 Apr 2019, 03:45 by Christophe Noël   [ updated 12 Apr 2019, 06:32 ]
A transaction is rolled back as soon as an exception is not caught. Nevertheless, even when it looks that everything is caught, the transaction roll back issue may happen:

NativeQuery is your enemy : a failed query could abort the JTA transaction (container managed transaction) even when catching any exception.

Some important tips:
  • @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) annotation on top of the problematic method would avoid propagating the rollback to the parent transaction. 
    • is useless if a uncatched exception is thrown to the parent method.
    • is useless if call was made from the same class instance !! (--> create another EJB)
  • Oracle doesn't throw an exception when you fail a query on a connection within a transaction and continue using that connection.
  • @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)

A good practice: if your query should not be rolled back when it fails, catch explicitely the PersistenceException:
 catch(PersistenceException e) {