Rollback for a Container managed transaction bean

Discussions

EJB programming & troubleshooting: Rollback for a Container managed transaction bean

  1. Hello all, I can't find a clear answer for this question neither in the spec nor on the forum. So, I have an EJB 2.1 using CMT. In which cases will the container rollback the transaction it started for the EJB? Does it depends on the type of exception encountered by the bean (SQLexception only for example)? For information, we plan to use stateless and statefull EJB 2 and before using CMT, we want to be sure to get the concept. thanks in advance for your answers, FFF
  2. Exception handling[ Go to top ]

    Referring to EJB spec 2.1, page 400, table 15 -> this contains the information you need. Basically, 'application-level' (checked) exceptions do NOT automatically lead to rollback. But I recommend reading the exception handling chapter to get the details. Best Guy http://www.atomikos.com Tired of EJB? Try JEE without application server :-)
  3. Hello all,
    I can't find a clear answer for this question neither in the spec nor on the forum.
    So, I have an EJB 2.1 using CMT. In which cases will the container rollback the transaction it started for the EJB? Does it depends on the type of exception encountered by the bean (SQLexception only for example)?
    For information, we plan to use stateless and statefull EJB 2 and before using CMT, we want to be sure to get the concept.

    thanks in advance for your answers,

    FFF
    Since you are using CMT, you should mark transaction for rollback using userTransaction.setRollbackOnly().
  4. RuntimeExceptions[ Go to top ]

    CMT transactions will automatically be rolled back for any RuntimeException. Additionally if you want to rollback on a checked exception, you need to catch the exception and set the rollback for transaction through sessioncontext. Only glitch here is in case of manual rollbacks (sessionContext.setRollback) the control is not abrupted rather the whole method executes but nothing will be committed when the method completes since the transaction is marked for roll back. So as a best practice it is recommended that when you design a session facade doin more than one JDBC update operations before each operation you must check for Rollback status. If the transaction is already set for rollback one need not execute the 2nd jdbc operation Eg. Pseudo Code // first jdbc operation try { }catch(NoAccountException ne) { sessionContext.setRollback; } if(!sessionContext.isRollback) { // Do Second Operation }
  5. Since you are using CMT, you should mark transaction for rollback using userTransaction.setRollbackOnly().
    CMT and userTransaction are incompatible.