Oracle8i Enterprise JavaBeans and CORBA Developer's Guide
Release 8.1.5

A64683-01

Library

Product

Contents

Index

Prev  Chap Top Next

EJB Transaction Examples

This section shows a few abbreviated examples of transaction management for EJB applications. For a set of complete programs, see "Transaction Examples".

Client-Side Demarcated

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.

Transaction Management in an EJB

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.

Getting the Session Context

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.

Beginning a Transaction

Invoke the begin() method as follows:

ctx.getUserTransaction.begin();

to start a transaction.

Committing a Transaction

Invoke the commit() method as follows:

ctx.getUserTransaction().commit();

to end the transaction with a commit.

Other UserTransaction Methods

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.




Prev

Top

Next
Oracle
Copyright © 1999 Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index