As a result of an error condition or exception, an application may determine that the current transaction should be rolled back. However, the application should not attempt to rollback the transaction directly. Instead, it should mark the transaction for rollback only, which sets a flag on the transaction indicating that the transaction cannot be committed.

When the time comes to end the transaction, the application server checks to see if the transaction is marked for rollback only, and if so, rolls back the transaction. If the rollback-only flag is not set, then the application server attempts to commit the transaction, which may result in a successful commit or in a rollback.

An application can also check whether a transaction has already been marked for rollback only. If so, the application should not attempt to enlist any further resources with the transaction. If a transaction has been marked for rollback only, then each subsequent attempt to obtain resources results in an error. Checking for rollback only can eliminate some of these errors and make debugging easier.

Setting and getting the rollback-only flag can be performed using the setRollbackOnly() and getStatus() methods of the UserTransaction interface. J2EE provides other interfaces for implementing these capabilities in specific component types. For example, the javax.ejb.EJBContext interface provides getRollbackOnly() and setRollbackOnly() methods to EJBs.

 
loading table of contents...