IBM DB2 Manual page 485

Table of Contents

Advertisement

transaction.
javax.transaction.UserTransaction utx;
// Use the begin method on a UserTransaction object to indicate
// the beginning of a distributed transaction.
utx.begin();
...
// Execute some SQL with one Connection object.
// Do not call Connection methods commit or rollback.
...
// Use the commit method on the UserTransaction object to
// drive all transaction branches to commit and indicate
// the end of the distributed transaction.
utx.commit();
...
Figure 57. Example of a distributed transaction under an application server
Figure 58 illustrates a program that uses JTA methods to execute a distributed
transaction. This program acts as the transaction manager and a transactional
application. Two connections to two different data sources do SQL work under a
single distributed transaction.
Figure 58. Example of a distributed transaction that uses the JTA
class XASample
{
Chapter 12. IBM Data Server Driver for JDBC and SQLJ type 4 connectivity JDBC and SQLJ distributed transaction support
javax.sql.XADataSource xaDS1;
javax.sql.XADataSource xaDS2;
javax.sql.XAConnection xaconn1;
javax.sql.XAConnection xaconn2;
javax.transaction.xa.XAResource xares1;
javax.transaction.xa.XAResource xares2;
java.sql.Connection conn1;
java.sql.Connection conn2;
public static void main (String args []) throws java.sql.SQLException
{
XASample xat = new XASample();
xat.runThis(args);
}
// As the transaction manager, this program supplies the global
// transaction ID and the branch qualifier. The global
// transaction ID and the branch qualifier must not be
// equal to each other, and the combination must be unique for
// this transaction manager.
public void runThis(String[] args)
{
byte[] gtrid = new byte[] { 0x44, 0x11, 0x55, 0x66 };
byte[] bqual = new byte[] { 0x00, 0x22, 0x00 };
int rc1 = 0;
int rc2 = 0;
try
{
javax.naming.InitialContext context = new javax.naming.InitialContext();
/*
* Note that javax.sql.XADataSource is used instead of a specific
* driver implementation such as com.ibm.db2.jcc.DB2XADataSource.
*/
xaDS1 = (javax.sql.XADataSource)context.lookup("checkingAccounts");
xaDS2 = (javax.sql.XADataSource)context.lookup("savingsAccounts");
469

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents