xaConn1 = xaDs1.getXAConnection();
trace("xacon2 = xads2.getXAConnection()");
xaConn2 = xaDs2.getXAConnection();
trace("xares1 = xacon1.getXAResource()");
XAResource xares1 = xaConn1.getXAResource();
trace("xares2 = xacon2.getXAResource()");
XAResource xares2 = xaConn2.getXAResource();
trace("xares1.recover(XAResource.TMSTARTRSCAN)");
Xid[] xids1 = xares1.recover(XAResource.TMSTARTRSCAN);
if ((xids1 == null) || (xids1.length == 0)) {
trace("xares1.recover(XAResource.TMSTARTRSCAN): 0");
} else {
trace("xares1.recover(XAResource.TMSTARTRSCAN): " + xids1.length);
}
trace("xares2.recover(XAResource.TMSTARTRSCAN)");
Xid[] xids2 = xares2.recover(XAResource.TMSTARTRSCAN);
if ((xids2 == null) || (xids2.length == 0)) {
trace("xares2.recover(XAResource.TMSTARTRSCAN): 0");
} else {
trace("xares2.recover(XAResource.TMSTARTRSCAN): " + xids2.length);
}
trace("con1 = xacon1.getConnection()");
conn1 = xaConn1.getConnection();
trace("stmt1 = con1.createStatement()");
stat1 = conn1.createStatement();
trace("con2 = xacon2.getConnection()");
conn2 = xaConn2.getConnection();
trace("stmt2 = con2.createStatement()");
stat2 = conn2.createStatement();
if (useOneDatabase) {
trace("stmt1.executeUpdate(\"DROP TABLE xatest1\")");
try {
stat1.executeUpdate("DROP TABLE xatest1");
} catch (SQLException e) {
// ignore
}
trace("stmt2.executeUpdate(\"DROP TABLE xatest2\")");
try {
stat2.executeUpdate("DROP TABLE xatest2");
} catch (SQLException e) {
// ignore
}
} else {
trace("stmt1.executeUpdate(\"DROP TABLE xatest\")");
try {
stat1.executeUpdate("DROP TABLE xatest");
} catch (SQLException e) {
// ignore
}
trace("stmt2.executeUpdate(\"DROP TABLE xatest\")");
try {
stat2.executeUpdate("DROP TABLE xatest");
} catch (SQLException e) {
// ignore
}
}
if (useOneDatabase) {
trace("stmt1.executeUpdate(\"CREATE TABLE xatest1 (id INT PRIMARY KEY, value INT)\")");
stat1.executeUpdate("CREATE TABLE xatest1 (id INT PRIMARY KEY, value INT)");
trace("stmt2.executeUpdate(\"CREATE TABLE xatest2 (id INT PRIMARY KEY, value INT)\")");
stat2.executeUpdate("CREATE TABLE xatest2 (id INT PRIMARY KEY, value INT)");
} else {
trace("stmt1.executeUpdate(\"CREATE TABLE xatest (id INT PRIMARY KEY, value INT)\")");
stat1.executeUpdate("CREATE TABLE xatest (id INT PRIMARY KEY, value INT)");
trace("stmt2.executeUpdate(\"CREATE TABLE xatest (id INT PRIMARY KEY, value INT)\")");
stat2.executeUpdate("CREATE TABLE xatest (id INT PRIMARY KEY, value INT)");
}
if (useOneDatabase) {
trace("stmt1.executeUpdate(\"INSERT INTO xatest1 VALUES (1, 0)\")");
stat1.executeUpdate("INSERT INTO xatest1 VALUES (1, 0)");
trace("stmt2.executeUpdate(\"INSERT INTO xatest2 VALUES (2, 0)\")");
stat2.executeUpdate("INSERT INTO xatest2 VALUES (2, 0)");
} else {
trace("stmt1.executeUpdate(\"INSERT INTO xatest VALUES (1, 0)\")");
stat1.executeUpdate("INSERT INTO xatest VALUES (1, 0)");
trace("stmt2.executeUpdate(\"INSERT INTO xatest VALUES (2, 0)\")");
stat2.executeUpdate("INSERT INTO xatest VALUES (2, 0)");
}
Xid xid1 = null;
Xid xid2 = null;
if (useOneDatabase) {
xid1 = SimpleXid.createRandom();
xid2 = SimpleXid.createRandom();
} else {
xid1 = SimpleXid.createRandom();
xid2 = xid1;
}
if (useOneDatabase) {
trace("xares1.start(xid1, XAResource.TMNOFLAGS)");
xares1.start(xid1, XAResource.TMNOFLAGS);
trace("xares2.start(xid2, XAResource.TMJOIN)");
xares2.start(xid2, XAResource.TMJOIN);
} else {
trace("xares1.start(xid1, XAResource.TMNOFLAGS)");
xares1.start(xid1, XAResource.TMNOFLAGS);
trace("xares2.start(xid2, XAResource.TMNOFLAGS)");
xares2.start(xid2, XAResource.TMNOFLAGS);
}
if (useOneDatabase) {
trace("stmt1.executeUpdate(\"UPDATE xatest1 SET value=1 WHERE id=1\")");
stat1.executeUpdate("UPDATE xatest1 SET value=1 WHERE id=1");
trace("stmt2.executeUpdate(\"UPDATE xatest2 SET value=1 WHERE id=2\")");
stat2.executeUpdate("UPDATE xatest2 SET value=1 WHERE id=2");
} else {
trace("stmt1.executeUpdate(\"UPDATE xatest SET value=1 WHERE id=1\")");
stat1.executeUpdate("UPDATE xatest SET value=1 WHERE id=1");
trace("stmt2.executeUpdate(\"UPDATE xatest SET value=1 WHERE id=2\")");
stat2.executeUpdate("UPDATE xatest SET value=1 WHERE id=2");
}
trace("xares1.end(xid1, XAResource.TMSUCCESS)");
xares1.end(xid1, XAResource.TMSUCCESS);
trace("xares2.end(xid2, XAResource.TMSUCCESS)");
xares2.end(xid2, XAResource.TMSUCCESS);
int ret1;
int ret2;
trace("ret1 = xares1.prepare(xid1)");
ret1 = xares1.prepare(xid1);
trace("xares1.prepare(xid1): " + ret1);
trace("ret2 = xares2.prepare(xid2)");
ret2 = xares2.prepare(xid2);
trace("xares2.prepare(xid2): " + ret2);
if ((ret1 != XAResource.XA_OK) && (ret1 != XAResource.XA_RDONLY)) {
throw new IllegalStateException("xares1.prepare(xid1) must return XA_OK or XA_RDONLY");
}
if ((ret2 != XAResource.XA_OK) && (ret2 != XAResource.XA_RDONLY)) {
throw new IllegalStateException("xares2.prepare(xid2) must return XA_OK or XA_RDONLY");
}
if (ret1 == XAResource.XA_OK) {
trace("xares1.commit(xid1, false)");
xares1.commit(xid1, false);
}
if (ret2 == XAResource.XA_OK) {
trace("xares2.commit(xid2, false)");
xares2.commit(xid2, false);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtils.closeSilently(stat1);