Oracle8i Enterprise JavaBeans and CORBA Developer's Guide Release 8.1.5 A64683-01 |
|
This section shows a few abbreviated examples of transaction management for EJB applications. For a set of complete programs, see "Transaction Examples".
If your EJB application requires client-side transaction demarcation, you use the JTS interface, as explained in "Using The Java Transaction Service". See the section "clientside" for a complete example of EJB client-side transaction demarcation.
Use the UserTransaction
interface to set up a transaction context within an EJB. In the bean implementation, make sure to import the javax.jts.UserTransaction
package. Unlike the TransactionService
when used on the client side, you do not need to initialize the UserTransaction
interface from within an EJB. The container does that for you.
In the EJB, use the setSessionContext()
session bean method to obtain the session context, and save it in an instance variable. For example, code this implementation of the setSessionContext()
method:
public class XBean implements SessionBean { SessionCtx ctx; ... public void setSessionContext(SessionContext ctx) { this.ctx = ctx; }
You can then use the session context ctx
to invoke UserTransaction
methods.
Invoke the begin()
method as follows:
ctx.getUserTransaction.begin();
to start a transaction.
Invoke the commit()
method as follows:
ctx.getUserTransaction().commit();
to end the transaction with a commit.
Invoke other methods of the UserTransaction
interface in the same way that you do a begin()
or a commit()
--invoke them on a UserTransaction
object of the session context.
See the section "serversideJTS" for a complete example that uses the UserTransaction
interface in an EJB.