Modified:
branches/branch_5_0/connector-j/src/com/mysql/jdbc/jdbc2/optional/ConnectionWrapper.java
branches/branch_5_0/connector-j/src/testsuite/simple/XATest.java
Log:
Fixed BUG#14279 - Idle timeouts cause XAConnections to whine about rolling
themselves back
Modified: branches/branch_5_0/connector-j/src/com/mysql/jdbc/jdbc2/optional/ConnectionWrapper.java
===================================================================
--- branches/branch_5_0/connector-j/src/com/mysql/jdbc/jdbc2/optional/ConnectionWrapper.java 2005-10-25 00:08:15 UTC (rev 4445)
+++ branches/branch_5_0/connector-j/src/com/mysql/jdbc/jdbc2/optional/ConnectionWrapper.java 2005-10-25 14:36:38 UTC (rev 4446)
@@ -769,7 +769,8 @@
return;
}
- if (this.mc.getRollbackOnPooledClose()
+ if (!this.isForXa
+ && this.mc.getRollbackOnPooledClose()
&& !this.getAutoCommit()) {
rollback();
}
Modified: branches/branch_5_0/connector-j/src/testsuite/simple/XATest.java
===================================================================
--- branches/branch_5_0/connector-j/src/testsuite/simple/XATest.java 2005-10-25 00:08:15 UTC (rev 4445)
+++ branches/branch_5_0/connector-j/src/testsuite/simple/XATest.java 2005-10-25 14:36:38 UTC (rev 4446)
@@ -51,8 +51,15 @@
this.xaDs = new MysqlXADataSource();
this.xaDs.setUrl(BaseTestCase.dbUrl);
+ this.xaDs.setRollbackOnPooledClose(true);
}
+ /**
+ * Tests that simple distributed transaction processing
+ * works as expected.
+ *
+ * @throws Exception if the test fails.
+ */
public void testCoordination() throws Exception {
if (!versionMeetsMinimum(5, 0)) {
return;
@@ -60,100 +67,155 @@
createTable("testCoordination", "(field1 int) ENGINE=InnoDB");
- XAConnection xaConn1 = getXAConnection();
- XAResource xaRes1 = xaConn1.getXAResource();
- Connection conn1 = xaConn1.getConnection();
+ Connection conn1 = null;
+ Connection conn2 = null;
+ XAConnection xaConn1 = null;
+ XAConnection xaConn2 = null;
- XAConnection xaConn2 = getXAConnection();
- XAResource xaRes2 = xaConn2.getXAResource();
- Connection conn2 = xaConn2.getConnection();
-
- Xid xid1 = createXid();
- Xid xid2 = createXid(xid1);
-
- xaRes1.start(xid1, XAResource.TMNOFLAGS);
- xaRes2.start(xid2, XAResource.TMNOFLAGS);
- conn1.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (1)");
- conn2.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (2)");
- xaRes1.end(xid1, XAResource.TMSUCCESS);
- xaRes2.end(xid2, XAResource.TMSUCCESS);
-
- xaRes1.prepare(xid1);
- xaRes2.prepare(xid2);
-
- xaRes1.commit(xid1, false);
- xaRes2.commit(xid2, false);
-
- this.rs = this.stmt.executeQuery("SELECT field1 FROM testCoordination ORDER BY field1");
-
- assertTrue(this.rs.next());
- assertEquals(1, this.rs.getInt(1));
-
- assertTrue(this.rs.next());
- assertEquals(2, this.rs.getInt(1));
-
- this.stmt.executeUpdate("TRUNCATE TABLE testCoordination");
-
- //
- // Now test rollback
- //
-
- xid1 = createXid();
- xid2 = createXid(xid1);
-
- xaRes1.start(xid1, XAResource.TMNOFLAGS);
- xaRes2.start(xid2, XAResource.TMNOFLAGS);
- conn1.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (1)");
-
- // ensure visibility
- assertEquals("1", getSingleIndexedValueWithQuery(conn1, 1, "SELECT field1 FROM testCoordination WHERE field1=1").toString());
-
- conn2.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (2)");
-
- // ensure visibility
- assertEquals("2", getSingleIndexedValueWithQuery(conn2, 1, "SELECT field1 FROM testCoordination WHERE field1=2").toString());
-
- xaRes1.end(xid1, XAResource.TMSUCCESS);
- xaRes2.end(xid2, XAResource.TMSUCCESS);
-
- xaRes1.prepare(xid1);
- xaRes2.prepare(xid2);
-
- xaRes1.rollback(xid1);
- xaRes2.rollback(xid2);
-
- this.rs = this.stmt.executeQuery("SELECT field1 FROM testCoordination ORDER BY field1");
-
- assertTrue(!this.rs.next());
+ try {
+ xaConn1 = getXAConnection();
+ XAResource xaRes1 = xaConn1.getXAResource();
+ conn1 = xaConn1.getConnection();
+
+ xaConn2 = getXAConnection();
+ XAResource xaRes2 = xaConn2.getXAResource();
+ conn2 = xaConn2.getConnection();
+
+ Xid xid1 = createXid();
+ Xid xid2 = createXid(xid1);
+
+ xaRes1.start(xid1, XAResource.TMNOFLAGS);
+ xaRes2.start(xid2, XAResource.TMNOFLAGS);
+ conn1.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (1)");
+ conn2.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (2)");
+ xaRes1.end(xid1, XAResource.TMSUCCESS);
+ xaRes2.end(xid2, XAResource.TMSUCCESS);
+
+ xaRes1.prepare(xid1);
+ xaRes2.prepare(xid2);
+
+ xaRes1.commit(xid1, false);
+ xaRes2.commit(xid2, false);
+
+ this.rs = this.stmt.executeQuery("SELECT field1 FROM testCoordination ORDER BY field1");
+
+ assertTrue(this.rs.next());
+ assertEquals(1, this.rs.getInt(1));
+
+ assertTrue(this.rs.next());
+ assertEquals(2, this.rs.getInt(1));
+
+ this.stmt.executeUpdate("TRUNCATE TABLE testCoordination");
+
+ //
+ // Now test rollback
+ //
+
+ xid1 = createXid();
+ xid2 = createXid(xid1);
+
+ xaRes1.start(xid1, XAResource.TMNOFLAGS);
+ xaRes2.start(xid2, XAResource.TMNOFLAGS);
+ conn1.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (1)");
+
+ // ensure visibility
+ assertEquals("1", getSingleIndexedValueWithQuery(conn1, 1, "SELECT field1 FROM testCoordination WHERE field1=1").toString());
+
+ conn2.createStatement().executeUpdate("INSERT INTO testCoordination VALUES (2)");
+
+ // ensure visibility
+ assertEquals("2", getSingleIndexedValueWithQuery(conn2, 1, "SELECT field1 FROM testCoordination WHERE field1=2").toString());
+
+ xaRes1.end(xid1, XAResource.TMSUCCESS);
+ xaRes2.end(xid2, XAResource.TMSUCCESS);
+
+ xaRes1.prepare(xid1);
+ xaRes2.prepare(xid2);
+
+ xaRes1.rollback(xid1);
+ xaRes2.rollback(xid2);
+
+ this.rs = this.stmt.executeQuery("SELECT field1 FROM testCoordination ORDER BY field1");
+
+ assertTrue(!this.rs.next());
+ } finally {
+ if (conn1 != null) {
+ conn1.close();
+ }
+
+ if (conn2 != null) {
+ conn2.close();
+ }
+
+ if (xaConn1 != null) {
+ xaConn1.close();
+ }
+
+ if (xaConn2 != null) {
+ xaConn2.close();
+ }
+ }
}
protected XAConnection getXAConnection() throws Exception {
return this.xaDs.getXAConnection();
}
+ /**
+ * Tests that XA RECOVER works as expected.
+ *
+ * @throws Exception if test fails
+ */
public void testRecover() throws Exception {
- if (!versionMeetsMinimum(5, 1)) {
+ if (!versionMeetsMinimum(5, 0)) {
return;
}
+
+ XAConnection xaConn = null, recoverConn = null;
- XAConnection xaConn = getXAConnection();
-
- Connection c = xaConn.getConnection();
- Xid xid = createXid();
-
- XAResource xaRes = xaConn.getXAResource();
- xaRes.start(xid, XAResource.TMNOFLAGS);
- c.createStatement().executeQuery("SELECT 1");
- xaRes.end(xid, XAResource.TMSUCCESS);
- xaRes.prepare(xid);
- xaConn.close();
-
- // Now try and recover
- XAResource recoverRes = getXAConnection().getXAResource();
-
- Xid[] recoveredXids = recoverRes.recover(XAResource.TMNOFLAGS);
-
- assertEquals(1, recoveredXids.length);
+ try {
+ xaConn = getXAConnection();
+
+ Connection c = xaConn.getConnection();
+ Xid xid = createXid();
+
+ XAResource xaRes = xaConn.getXAResource();
+ xaRes.start(xid, XAResource.TMNOFLAGS);
+ c.createStatement().executeQuery("SELECT 1");
+ xaRes.end(xid, XAResource.TMSUCCESS);
+ xaRes.prepare(xid);
+
+ // Now try and recover
+ recoverConn = getXAConnection();
+
+ XAResource recoverRes = recoverConn.getXAResource();
+
+ Xid[] recoveredXids = recoverRes.recover(XAResource.TMNOFLAGS);
+
+ assertTrue(recoveredXids != null);
+ assertTrue(recoveredXids.length > 0);
+
+ boolean xidFound = false;
+
+ for (int i = 0; i < recoveredXids.length; i++) {
+ if (recoveredXids[i] != null &&
+ recoveredXids[i].equals(xid)) {
+ xidFound = true;
+
+ break;
+ }
+ }
+
+ assertTrue(xidFound);
+ } finally {
+ if (xaConn != null) {
+ xaConn.close();
+ }
+
+ if (recoverConn != null) {
+ recoverConn.close();
+ }
+ }
}
private Xid createXid() throws IOException {
| Thread |
|---|
| • Connector/J commit: r4446 - in branches/branch_5_0/connector-j/src: com/mysql/jdbc/jdbc2/optional testsuite/simple | mmatthews | 25 Oct |