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 interface 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:

// get the JNDI InitialContext object 

InitialContext context = new javax.naming.InitialContext(); 

// look up the UserTransaction and DataSource to DBMS 

UserTransaction ut = (UserTransaction)
    context.lookup("sssw://" + servPort + "/java:comp/UserTransaction");
DataSource source = (DataSource)
    context.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 = source.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 = ...;  // an 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 transaction, the transaction can be completed with rollback or commit:

// commit the work in this transaction:

tm.commit();

Managing Transaction 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 transaction ... 

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 © 2003, 2004 Novell, Inc. All rights reserved. Copyright © 2001, 2002, 2003 SilverStream Software, LLC. All rights reserved.