*/
private static void interleavingTransactions(XADataSource xads) {
System.out.println("interleavingTransactions");
try {
XAConnection xac = xads.getXAConnection("sku", "testxa");
XAResource xar = xac.getXAResource();
Xid xid1 = XATestUtil.getXid(1, 93, 18);
Xid xid2 = XATestUtil.getXid(2, 45, 77);
xar.start(xid1, XAResource.TMNOFLAGS);
Connection conn = xac.getConnection();
Statement s = conn.createStatement();
s.executeUpdate("insert into APP.foo values (1)");
xar.end(xid1, XAResource.TMSUSPEND);
xar.start(xid2, XAResource.TMNOFLAGS);
s.executeUpdate("insert into APP.foo values (2)");
xar.end(xid2, XAResource.TMSUSPEND);
xar.start(xid1, XAResource.TMRESUME);
s.executeUpdate("insert into APP.foo values (3)");
xar.end(xid1, XAResource.TMSUSPEND);
xar.start(xid2, XAResource.TMRESUME);
s.executeUpdate("insert into APP.foo values (4)");
XATestUtil.showXATransactionView(conn);
// this prepare won't work since
// transaction 1 has been suspended - XA_PROTO
try {
xar.prepare(xid1);
System.out.println("FAIL - prepare on suspended transaction");
} catch (XAException e) {
if (e.errorCode != XAException.XAER_PROTO)
XATestUtil.dumpXAException(
"FAIL - prepare on suspended transaction", e);
}
// check it was not prepared
XATestUtil.showXATransactionView(conn);
xar.end(xid2, XAResource.TMSUCCESS);
xar.end(xid1, XAResource.TMSUCCESS);
xar.prepare(xid1);
xar.prepare(xid2);
// both should be prepared.
XATestUtil.showXATransactionView(conn);
Xid[] recoveredStart = xar.recover(XAResource.TMSTARTRSCAN);
System.out.println("recovered start " + recoveredStart.length);
Xid[] recovered = xar.recover(XAResource.TMNOFLAGS);
System.out.println("recovered " + recovered.length);
Xid[] recoveredEnd = xar.recover(XAResource.TMENDRSCAN);
System.out.println("recovered end " + recoveredEnd.length);
for (int i = 0; i < recoveredStart.length; i++) {
Xid xid = recoveredStart[i];
if (xid.getFormatId() == 1) {
// commit 1 with 2pc
xar.commit(xid, false);
} else if (xid.getFormatId() == 2) {
xar.rollback(xid);
} else {
System.out.println("FAIL: unknown xact");
}
}
// check the results
Xid xid3 = XATestUtil.getXid(3, 2, 101);
xar.start(xid3, XAResource.TMNOFLAGS);
XATestUtil.showXATransactionView(conn);
ResultSet rs = s.executeQuery("select * from APP.foo");
JDBCDisplayUtil.DisplayResults(System.out, rs, conn);
rs.close();
xar.end(xid3, XAResource.TMSUCCESS);
int pr = xar.prepare(xid3);
if (pr != XAResource.XA_RDONLY)
System.out.println("FAIL - prepare on read only xact returned "
+ pr);
try {
xar.commit(xid3, true);
System.out.println("FAIL - 2pc commit on read-only xact");
} catch (XAException e) {
if (e.errorCode != XAException.XAER_NOTA)
throw e;
}