11.2. Bean-managed Transactions

A bean that manages its transactions itself must set the transaction-type element in its standard deployment descriptor to:

<transaction-type>Bean</transaction-type>

To demarcate the transaction boundaries in a bean with bean-managed transactions, the bean programmer should use the javax.transaction.UserTransaction interface, which is defined on an EJB server object that may be obtained using the EJBContext.getUserTransaction() method (the SessionContext object or the EntityContext object depending on whether the method is defined on a session or on an Entity Bean).

The following example shows a Session Bean method doTxJob demarcating the transaction boundaries; the UserTransaction object is obtained from the sessionContext object, which should have been initialized in the setSessionContext method (refer to the Section 7.4 The Enterprise Bean Class).

public void doTxJob() throws  RemoteException {
     UserTransaction ut = sessionContext.getUserTransaction();
     ut.begin();
     ... // transactional operations
     ut.commit();
}

Another way to do this is to use JNDI and to retrieve UserTransaction with the name java:comp/UserTransaction in the initial context.