This topic describes how to enable a session bean or servlet to use bean-managed transactions, to manage its own transactions directly instead of letting the container manage the transactions.
Why and when to perform this task
Note: Entity beans cannot manage transactions (so cannot use bean-managed transactions).
To enable a session bean or servlet to use bean-managed transactions, complete the following steps:
Steps for this task
The following code extract shows the standard code required to obtain an object encapsulating the transaction context. There are three basics steps involved:
... import javax.transaction.*; ... public class MyStatelessSessionBean implements SessionBean { private SessionContext mySessionCtx =null; ... public void setSessionContext (SessionContext ctx)throws EJBException { mySessionCtx =ctx; } ... public float doSomething(long arg1)throws FinderException,EJBException { UserTransaction userTran = (UserTransaction)initCtx.lookup( "java:comp/UserTransaction"); ... //User userTran object to call transaction methods userTran.begin (); //Do transactional work ... userTran.commit (); ... } ... }