The JDBC XA distributed transaction management classes enable you to use the IBM® Toolbox for Java™ JDBC driver within a distributed transaction. Using the XA classes to enable the IBM Toolbox for Java JDBC driver allows it to participate in transactions that span multiple data sources.
Typically, XA distributed transaction management classes are used and controlled directly by a transaction manager, which is separate from the JDBC driver. The distributed transaction management interfaces are defined as part of the JDBC 2.0 Optional Package and the Java Transaction API (JTA). Both are available from Sun as jar files. The distributed transaction management interfaces are also supported in the JDBC 3.0 API, which is bundled with the Java 2 Platform, Standard Edition, version 1.4.
For more information, see the Sun Web sites for JDBC and the JTA .
Use the following objects to enable the IBM Toolbox for Java JDBC driver to participate in XA distributed transactions:
The following example shows simple usage of the XA classes. Keep in mind that the details would be filled in with work using other data sources. This type of code usually appears within a transaction manager.
// Create an XA data source for making the XA connection. AS400JDBCXADataSource xaDataSource = new AS400JDBCXADataSource("myAS400"); xaDataSource.setUser("myUser"); xaDataSource.setPassword("myPasswd"); // Get an XAConnection and get the associated XAResource. // This provides access to the resource manager. XAConnection xaConnection = xaDataSource.getXAConnection(); XAResource xaResource = xaConnection.getXAResource(); // Generate a new Xid (this is up to the transaction manager). Xid xid = ...; // Start the transaction. xaResource.start(xid, XAResource.TMNOFLAGS); // ...Do some work with the database... // End the transaction. xaResource.end(xid, XAResource.TMSUCCESS); // Prepare for a commit. xaResource.prepare(xid); // Commit the transaction. xaResource.commit(xid, false); // Close the XA connection when done. This implicitly // closes the XA resource. xaConnection.close();