Using the JTA API

The TimesTen implementation of JTA provides an API consistent with that specified in the JTA specification.

This section covers the following topics for using the JTA API:

Registering a TimesTen DSN with WebLogic

Regarding how to register a TimesTen DSN with WebLogic, information on configuring TimesTen for application servers and object-relational mapping frameworks is available in the TimesTen Classic Quick Start.

See About TimesTen Quick Start and Sample Applications.

Importing Required Packages

The TimesTen JDBC and XA implementations are available in packages.

import com.timesten.jdbc.*;
import com.timesten.jdbc.xa.*;

Your application should also import these standard packages:

import java.sql.*;
import javax.sql.*;
import javax.transaction.xa.*;

Creating a TimesTen XAConnection Object

Connections to XA data sources are established through XADataSource objects. You can create an XAConnection object for your database by using the TimesTenXADataSource instance as a connection factory. TimesTenXADataSource implements the javax.sql.XADataSource interface.

After creating a new TimesTenXADataSource instance, use the setUrl() method to specify a database.

The URL should look similar to the following.

  • For a direct connection: jdbc:timesten:direct:DSNname

  • For a client connection: jdbc:timesten:client:DSNname

You can also optionally use the setUser() and setPassword() methods to set the ID and password for a specific user.

In the following example, the TimesTenXADataSource object is used as a factory to create a TimesTen XA data source object. Then the URL that identifies the TimesTen DSN (dsn1), the user name (myName), and the password (password) are set for this TimesTenXADataSource instance. Then the getXAConnection() method is used to return a connection to the object, xaConn.

TimesTenXADataSource xads = new TimesTenXADataSource();
 
xads.setUrl("jdbc:timesten:direct:dsn1");
xads.setUser("myName");
xads.setPassword("password");
 
XAConnection xaConn = null;
try {
    xaConn = xads.getXAConnection();
}
catch (SQLException e){
    e.printStackTrace();
    return;
}

You can create multiple connections to an XA data source object. This example creates a second connection, xaConn2:

XAConnection xaConn  = null;
XAConnection xaConn2 = null;

try {
    xaConn  = xads.getXAConnection();
    xaConn2 = xads.getXAConnection();
}

This next example creates two instances of TimesTenXADataSource for the databases named dsn1 and dsn2. It then creates a connection for dsn1 and two connections for dsn2.

TimesTenXADataSource xads = new TimesTenXADataSource();
 
xads.setUrl("jdbc:timesten:direct:dsn1");
xads.setUser("myName");
xads.setPassword("password");
 
XAConnection xaConn1 = null;
XAConnection xaConn2 = null;
XAConnection xaConn3 = null;
 
try {
   xaConn1 = xads.getXAConnection(); // connect to dsn1
}
catch (SQLException e){
   e.printStackTrace();
   return;
}
 
xads.setUrl("jdbc:timesten:direct:dsn2");
xads.setUser("myName");
xads.setPassword("password");
 
try {
   xaConn2 = xads.getXAConnection(); // connect to dsn2
   xaConn3 = xads.getXAConnection(); // connect to dsn2
}
catch (SQLException e){
   e.printStackTrace();
   return;
}

Note:

Once an XAConnection is established, autocommit is turned off.

Creating XAResource and Connection Objects

After using getXAConnection() to obtain an XAConnection object, you can use the XAConnection method getXAResource() to obtain an XAResource object, then the XAConnection method getConnection() to obtain a Connection object for the underlying connection.

//get an XAResource
XAResource xaRes = null;
try {
   xaRes = xaConn.getXAResource();
}
catch (SQLException e){
   e.printStackTrace();
   return;
}
 
//get an underlying physical Connection
Connection conn = null;
try {
   conn = xaConn.getConnection();
}
catch (SQLException e){
   e.printStackTrace();
   return;
}

From this point, you can use the same connection, conn, for both local and global transactions. Be aware of the following, however.

  • You must commit or roll back an active local transaction before starting a global transaction. Otherwise you get the XAException exception XAER_OUTSIDE.

  • You must end an active global transaction before initiating a local transaction, otherwise you get a SQLException, "Illegal combination of local transaction and global (XA) transaction."