EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #486 background imageLoading...
Page #486 background image
// The XADatasource contains the user ID and password.
// Get the XAConnection object from each XADataSource
xaconn1 = xaDS1.getXAConnection();
xaconn2 = xaDS2.getXAConnection();
// Get the java.sql.Connection object from each XAConnection
conn1 = xaconn1.getConnection();
conn2 = xaconn2.getConnection();
// Get the XAResource object from each XAConnection
xares1 = xaconn1.getXAResource();
xares2 = xaconn2.getXAResource();
// Create the Xid object for this distributed transaction.
// This example uses the com.ibm.db2.jcc.DB2Xid implementation
// of the Xid interface. This Xid can be used with any JDBC driver
// that supports JTA.
javax.transaction.xa.Xid xid1 =
new com.ibm.db2.jcc.DB2Xid(100, gtrid, bqual);
// Start the distributed transaction on the two connections.
// The two connections do NOT need to be started and ended together.
// They might be done in different threads, along with their SQL operations.
xares1.start(xid1, javax.transaction.xa.XAResource.TMNOFLAGS);
xares2.start(xid1, javax.transaction.xa.XAResource.TMNOFLAGS);
...
// Do the SQL operations on connection 1.
// Do the SQL operations on connection 2.
...
// Now end the distributed transaction on the two connections.
xares1.end(xid1, javax.transaction.xa.XAResource.TMSUCCESS);
xares2.end(xid1, javax.transaction.xa.XAResource.TMSUCCESS);
// If connection 2 work had been done in another thread,
// a thread.join() call would be needed here to wait until the
// connection 2 work is done.
try
{ // Now prepare both branches of the distributed transaction.
// Both branches must prepare successfully before changes
// can be committed.
// If the distributed transaction fails, an XAException is thrown.
rc1 = xares1.prepare(xid1);
if(rc1 == javax.transaction.xa.XAResource.XA_OK)
{ // Prepare was successful. Prepare the second connection.
rc2 = xares2.prepare(xid1);
if(rc2 == javax.transaction.xa.XAResource.XA_OK)
{ // Both connections prepared successfully and neither was read-only.
xares1.commit(xid1, false);
xares2.commit(xid1, false);
}
else if(rc2 == javax.transaction.xa.XAException.XA_RDONLY)
{ // The second connection is read-only, so just commit the
// first connection.
xares1.commit(xid1, false);
}
}
else if(rc1 == javax.transaction.xa.XAException.XA_RDONLY)
{ // SQL for the first connection is read-only (such as a SELECT).
// The prepare committed it. Prepare the second connection.
rc2 = xares2.prepare(xid1);
if(rc2 == javax.transaction.xa.XAResource.XA_OK)
{ // The first connection is read-only but the second is not.
// Commit the second connection.
xares2.commit(xid1, false);
}
else if(rc2 == javax.transaction.xa.XAException.XA_RDONLY)
{ // Both connections are read-only, and both already committed,
470 Application Programming Guide and Reference for Java
â„¢

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability