0
votes

Environment: Java 8, JEE 7 (no Spring), JBoss 7 GA, EJBs and ManagedBeans, Informix and Oracle DB, using default CMT.

I found the following statement regarding unchecked exception and its rollback behavior:

"[...] unchecked Exceptions (RuntimeExceptions) will usually cause a rollback of a transaction (unless annotated as @AppliationException(rollback=false)"

So now the javadoc of the following unchecked exceptions

  • javax.persistence.NonUniqueResultException.class
  • javax.persistence.NoResultException.class

state:

"This exception will not cause the current transaction, if one is active, to be marked for rollback."

Weirdly the NonUniqueResultException did cause my transaction to be rolled back. So I added the following lines to the method:

@javax.transaction.Transactional(dontRollbackOn = {javax.persistence.NonUniqueResultException.class, javax.persistence.NoResultException.class})

Additionally I wrapped these exceptions with my custom unchecked exception (... MyOwnException extends RuntimeException) but I am uncertain of the default behavior:

Three questions: (1) When my own custom exception is thrown, does it rollback the current transaction or not? (It is not annoted with @AppliationException) (2) What is the JEE spec compliant behavior (since obviously the real world JEE implementation differ)? (3) And in the case the transaction would be marked for rollback and I have quite a call hierarchy do I have to add dontRollbackOn at each method or only once per transaction?

1

1 Answers

0
votes

The Java EE specification says that an unchecked exception will cause a rollback of the active transaction. So, if your custom exception extends RuntimeException they will cause a rollback. In such case, the active transaction will be rolled back, no matter how deep you call hierarchy is. All the modifications done in the same transaction boundaries will be affected by the rollback.

On the other hand, if your exception is a checked one it won't cause the transaction rollback unless you annotate it as @ApplicationException(rollback = true).