This topic describes how to enable a session bean, servlet, or application client component to use component-managed transactions, to manage its own transactions directly instead of letting the container manage the transactions.
Why and when to perform this task
To enable a session bean, servlet, or application client component to use component-managed transactions, complete the following steps:
Steps for this task
For stateful session beans, a transaction started in a given method does not need to be completed (that is, committed or rolled back) before completing that method. The transaction can be completed at a later time, for example on a subsequent call to the same method, or even within a different method. However, constructing the application so a transaction is begun and completed within the same method call is usually preferred, because it simplifies application debugging and maintenance.
... import javax.transaction.*; import javax.naming.InitialContext; import javax.naming.NamingException; ... public float doSomething(long arg1)throws NamingException { InitialContext initCtx = new InitialContext(); UserTransaction userTran = (UserTransaction)initCtx.lookup( "java:comp/UserTransaction"); ... //Use userTran object to call transaction methods userTran.begin (); //Do transactional work ... userTran.commit (); ... } ... }
Related concepts
Client support for transactions
Related tasks
Developing a component to use transactions
Related reference
UserTransaction interface - methods available
Using local transactions