Discussions

EJB programming & troubleshooting: Transactional Rollbacks

  1. Transactional Rollbacks (2 messages)

    I have a simple question that does not seem to have a simple answer since I hear different things from different people. Simply put, I would like to know what steps need to be taken in rolling back a transaction. I believe that any subclass of RuntimeException, most notably EJBException, triggers a rollback, but what does that mean exactly? Does throwing the exception by itself cause the rollback? Or does one have to explicitly call setRollbackOnly() on the SessionContext? And if the latter is the case, what is the point then of CMT? If I have to write transactional code anyway, I could just as easily write it in BMT, couldn't I?

    Any guidance is appreciated. Thanks.

    Threaded Messages (2)

  2. Transactional Rollbacks[ Go to top ]

    It depends if you want to do Transaction programmatically or declaratively.

    Declaratively:
    you do it in the deployment descriptor and how (Read EJB tutorial from Javasoft website).

    Programmatically:
    UserTransaction tr = context.getUserTransaction();
    tr.begin(); //to begin a transaction
    tr.commit(); // to commit a transaction

    tr.setrollbackonly(); //it finally cause roll back
    tr.rollback(); //rolls back the transaction

    if exception is caught while in transaction you usually call tr.rollback(); in try catch block.

    look up the API for UserTransaction and Entitycontext also sessionContext to get more on this.
    .
  3. Transactional Rollbacks[ Go to top ]

    Neil,

    Using CMT, rollback is automatic when a system exception(Runtime) is thrown. However, if you are catching some exception, e.g. SQLException, and then want to force a rollback, in that case you call context.setRollbackOnly() before throwing an Exception like (CreateException, RemoveException, EJBException.)

    Mike