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.
-
Transactional Rollbacks (2 messages)
- Posted by: Neil Chaudhuri
- Posted on: November 12 2002 15:58 EST
Threaded Messages (2)
- Transactional Rollbacks by Web Master on November 12 2002 16:29 EST
- Transactional Rollbacks by Michael Malkinzon on November 13 2002 11:32 EST
-
Transactional Rollbacks[ Go to top ]
- Posted by: Web Master
- Posted on: November 12 2002 16:29 EST
- in response to Neil Chaudhuri
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.
. -
Transactional Rollbacks[ Go to top ]
- Posted by: Michael Malkinzon
- Posted on: November 13 2002 11:32 EST
- in response to Neil Chaudhuri
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