This is an example of how to use multiple connections working on a single transaction.
import java.sql.*; import javax.sql.*; import java.util.*; import javax.transaction.*; import javax.transaction.xa.*; import com.ibm.db2.jdbc.app.*; public class JTAMultiConn { public static void main(java.lang.String[] args) { JTAMultiConn test = new JTAMultiConn(); test.setup(); test.run(); } /** * Handle the previous cleanup run so that this test can recommence. */ public void setup() { Connection c = null; Statement s = null; try { Class.forName("com.ibm.db2.jdbc.app.DB2Driver"); c = DriverManager.getConnection("jdbc:db2:*local"); s = c.createStatement(); try { s.executeUpdate("DROP TABLE CUJOSQL.JTATABLE"); } catch (SQLException e) { // Ignore... does not exist } s.executeUpdate("CREATE TABLE CUJOSQL.JTATABLE (COL1 CHAR (50))"); s.close(); } finally { if (c != null) { c.close(); } } } /** * This test uses JTA support to handle transactions. */ public void run() { Connection c1 = null; Connection c2 = null; Connection c3 = null; try { Context ctx = new InitialContext(); // Assume the data source is backed by a UDBXADataSource. UDBXADataSource ds = (UDBXADataSource) ctx.lookup("XADataSource"); // From the DataSource, obtain some XAConnection objects that // contain an XAResource and a Connection object. XAConnection xaConn1 = ds.getXAConnection(); XAConnection xaConn2 = ds.getXAConnection(); XAConnection xaConn3 = ds.getXAConnection(); XAResource xaRes1 = xaConn1.getXAResource(); XAResource xaRes2 = xaConn2.getXAResource(); XAResource xaRes3 = xaConn3.getXAResource(); c1 = xaConn1.getConnection(); c2 = xaConn2.getConnection(); c3 = xaConn3.getConnection(); Statement stmt1 = c1.createStatement(); Statement stmt2 = c2.createStatement(); Statement stmt3 = c3.createStatement(); // For XA transactions, a transaction identifier is required. // Support for creating XIDs is again left to the application // program. Xid xid = JDXATest.xidFactory(); // Perform some transactional work under each of the three // connections that have been created. xaRes1.start(xid, XAResource.TMNOFLAGS); int count1 = stmt1.executeUpdate("INSERT INTO " + tableName + "VALUES('Value 1-A')"); xaRes1.end(xid, XAResource.TMNOFLAGS); xaRes2.start(xid, XAResource.TMJOIN); int count2 = stmt2.executeUpdate("INSERT INTO " + tableName + "VALUES('Value 1-B')"); xaRes2.end(xid, XAResource.TMNOFLAGS); xaRes3.start(xid, XAResource.TMJOIN); int count3 = stmt3.executeUpdate("INSERT INTO " + tableName + "VALUES('Value 1-C')"); xaRes3.end(xid, XAResource.TMSUCCESS); // When completed, commit the transaction as a single unit. // A prepare() and commit() or 1 phase commit() is required for // each separate database (XAResource) that participated in the // transaction. Since the resources accessed (xaRes1, xaRes2, and xaRes3) // all refer to the same database, only one prepare or commit is required. int rc = xaRes.prepare(xid); xaRes.commit(xid, false); } catch (Exception e) { System.out.println("Something has gone wrong."); e.printStackTrace(); } finally { try { if (c1 != null) { c1.close(); } } catch (SQLException e) { System.out.println("Note: Cleaup exception " + e.getMessage()); } try { if (c2 != null) { c2.close(); } } catch (SQLException e) { System.out.println("Note: Cleaup exception " + e.getMessage()); } try { if (c3 != null) { c3.close(); } } catch (SQLException e) { System.out.println("Note: Cleaup exception " + e.getMessage()); } } } }