Overview of JTA

The Novell exteNd Messaging Platform's JTS provides an implementation of the JTA TransactionManager and UserTransaction interfaces. These interfaces represent the contract between the transaction manager and the application server, and between the transaction manager and the user application on an J2EE application server.

Starting transactions with UserTransaction

The UserTransaction provides a high level transaction demarcation for user applications.
Starting transactions with TransactionManager
The TransactionManager is the interface provides more functionality for direct transaction management, and thus can be more difficult to use. This section discusses:

Starting transactions with UserTransaction

The UserTransaction is the interface between user application and the transaction manager. It only provides transaction demarcation calls, it does not provide any means for enlisting resources in transactions, and it keeps track of thread to transaction association. The application code normally uses the data sources provide by the application server to get connection to the underlying DBMS, while the application server performs all the necessary calls to associate the connection with the current active transaction. The following is a code sample of how UserTransaction is used in a J2EE application, such as a Servlet, or an EJB Session Bean that manages its own transactions:
UserTransaction ut;
DataSource  ds;
InitialContext ICl;

// get the JNDI InitialContext object
//
IC = new javax.naming.InitialContext();

// look up the UserTransaction and DataSource to DBMS
//
ut = (UserTransaction)IC.lookup("sssw://"+servPort+"/java:comp/UserTransaction");
ds = (DataSource)IC.lookup("lookup name for AppSever provided DataSource");

// Begin a transaction
ut.begin();

// Get a java.sql.Connection from DataSource
// The Application Server will ensure that this connection is properly
// enlisted in the transaction via the XAResource interface.
//
Connection conn = ds.getConnection();

// Make updates to the database  ...

conn.close();

// Commit the transaction
ut.commit();
 

Starting transactions with TransactionManager

The TransactionManager is the interface between the application server and the transaction manager in J2EE platform. It provides transaction demarcation, control for associating resources to transactions, and control for associating threads with transactions.

Once JTS is instantiated, set as default transaction service of the ORB, and recovered, it can be used for managing transactions in an application. (see section Creating a Server TM for details)
 

Associating one or more XA capable resources in a transaction

The following is sample code that shows how to get reference to TransactionManager object and start a transaction:
 
import javax.transaction.TransactionManager;
import com.sssw.jts.api.TransactionService;

// JTS and ORB initialization ...
    ...

// Find initial reference to TransactionService object
//
TransactionService ts = (TransactionService)
    orb.resolve_initial_references("TransactionService");
TransactionManager tm = ts.getTransactionManager();
tm.begin();


The following is sample code that shows how to associate a XA capable resource with an active transaction:

import java.sql.*;
import javax.sql.*;

// Get a XAConnection from a XADataSource object.
//
XADataSource    xadsA = ...;  // A XA capable JDBC 2.0 DataSource
XAConnection    xaconnA;      // XAConnection from XADataSource
XAResource      xaresA;       //
Connection      connA;
Statement       statement;

xaconnA = XADataSource.getXAConnection();

// Get the XAResource and enlist it in the current transaction
//
xaresA = xaconnA.getXAResource();
tm.getTransaction().enlistResource(xaresA);

// Now get the Connection from XAConnection and do updates to database.
//
connA = XAConnection.getConnection();
statement = connA.createStatement();
statement.executeUpdate("INSERT INTO ... ");
statement.close();
connA.close();

Once the work to be associated with the current transaction in the XA capable connection is completed, The XAResource can be delisted from the transaction.  There are two ways of delisting, one way is to suspend the XAResource to transaction association, and the other is to end the association.  If an XAResource's association is suspended, then it can be resumed in the same transaction, which means that more database updates can be associated to the transaction.  The following is sample code that shows how to end an association of an XAResource with an active transaction:
// Were done with updates using this connection...delist from Transaction
//
tm.getTransaction().delistResource(xaresA, XAResource.TMSUCCESS);
Multiple resources can be associated with the current transaction and once all work is completed in all the resources that were associated with the transaciton, the transaction can be completed with rollback or commit:
 
// Commit the work in this transaction;
tm.commit();

Managing transaciton to thread association

The TransactionManager interface provides functionality to allow managing thread to transaction association.  The restriction is that only one transaction can be active in a thread, i.e. JTS does not support nested transaction.

TransactionManager tm = ts.getTransactionManager();

// Start a transaciton ...
//
tm.begin();

// Do some work in this transaction
    ...

// Suspend the first transaction
Transaction firstTrans = tm.suspend();

// Start a second transaction
tm.begin();

// Do some work in this transaction
    ...

// Suspend the second transaction
Transaction secondTrans = tm.suspend();

// Resume and complete the first transaction
tm.resume(firstTrans);
tm.commit();

// Resume and complete the first transaction
tm.resume(secondTrans);
tm.commit();



Copyright © 1998-2003, Novell, Inc. All rights reserved.