Getting Started with Enterprise JavaBeans™

G

Entity Beans

Tutorial Home Section Home Previous Section Next

Get

Get

Controlling Transactions

     Bean-managed transactions

     Session and message-driven beans can declare that transactions are bean-managed and use the Java Transaction API directly for transaction demarcation and control. The interface for JTA is javax.transaction.UserTransaction, which is accessed via an EJBContext object. EJBContext is the parent of EntityContext, MessageDrivenContext, and SessionContext, which are obtained from the appropriate setXXXContext method. Once the UserTransaction object is obtained, the bean can invoke the begin(), commit(), and rollback() methods for transaction control. When using JTA, the developer must not use any resource manager methods for transactions (JDBC commits or rollbacks, for example), but instead must use only UserTransaction methods.

     Typical code to access and use the UserTransaction follows:


javax.transaction.UserTransaction ut = 
   ejbContext.getUserTransaction();
// start the transaction
ut.begin();
// Do some work.
if( aGoodResult )
{
  // commit the transaction
  ut.commit();
}
else
{
  // roll back the transaction
  ut.rollback();
}

     To enable bean-managed transactions (BMT), you must ensure that the deployment descriptor contains a <transaction-type>Bean</transaction-type> entry.


     Container-managed transactions

     Any bean can use container-managed transaction handling (CMT), which is completely declarative (there is no indication in the code that transactions are being used), by means of a <transaction-type>Container</transaction-type> entry in the deployment descriptor. The container's responsibility for CMT is declared in the deployment descriptor with a <trans-attribute></trans-attribute> pair enclosing one of the following transaction attributes:

     The Required attribute is a safe choice available to all EJB components: if a transaction is in progress when an operation is performed, the operation joins it; if not, a new transaction is started. The other attributes provide transactional variations, but are restricted to specific bean types.

     A system exception will automatically cause a transaction rollback. This is not the case for application level exceptions. If an application exception occurs that affects data integrity or otherwise causes the transaction to be invalid, under CMT the exception handler should invoke the EJBContext.setRollbackOnly() method. As noted in the javax.ejb.EJBContext API documentation, EJBContext.setRollbackOnly() will "mark the current transaction for rollback.... A transaction marked for rollback can never commit. Only enterprise beans with container-managed transactions are allowed to use this method."

     For more information about javax.transaction.UserTransaction and transaction attributes, see Chapter 17, "Support for Transactions," in the EJB 2.0 specification, and the other JTA/JTS material in Resources.



Tutorial Home Section Home Previous Section Next