[libpostgresql-jdbc-java] 03/08: A XA transaction should not change the autocommit setting of a Connection. Ensure that we restore this property correctly after the XA transaction completes.
Emmanuel Bourg
ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:20:38 UTC 2017
This is an automated email from the git hooks/post-receive script.
ebourg-guest pushed a commit to tag REL8_2_511
in repository libpostgresql-jdbc-java.
commit e864bb15ec74cb7fc9d8b1b1f930e815244790b4
Author: Kris Jurka <books at ejurka.com>
Date: Sat May 1 14:41:11 2010 +0000
A XA transaction should not change the autocommit setting of a
Connection. Ensure that we restore this property correctly after
the XA transaction completes.
Heikki Linnakangas
Test case from Achilleas Mantzios
---
org/postgresql/test/xa/XADataSourceTest.java | 12 ++++++++++++
org/postgresql/xa/PGXAConnection.java | 17 ++++++++++++++---
2 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/org/postgresql/test/xa/XADataSourceTest.java b/org/postgresql/test/xa/XADataSourceTest.java
index 3b1bc1d..7238107 100644
--- a/org/postgresql/test/xa/XADataSourceTest.java
+++ b/org/postgresql/test/xa/XADataSourceTest.java
@@ -307,6 +307,18 @@ public class XADataSourceTest extends TestCase {
xaRes.commit(xid, true);
}
+ public void testRestoreOfAutoCommit() throws Exception {
+ conn.setAutoCommit(false);
+
+ Xid xid = new CustomXid(14);
+ xaRes.start(xid, XAResource.TMNOFLAGS);
+ xaRes.end(xid, XAResource.TMSUCCESS);
+ xaRes.commit(xid, true);
+
+ assertTrue(!conn.getAutoCommit());
+ }
+
+
/* We don't support transaction interleaving.
public void testInterleaving1() throws Exception {
Xid xid1 = new CustomXid(1);
diff --git a/org/postgresql/xa/PGXAConnection.java b/org/postgresql/xa/PGXAConnection.java
index cba69cc..e64cde2 100644
--- a/org/postgresql/xa/PGXAConnection.java
+++ b/org/postgresql/xa/PGXAConnection.java
@@ -65,6 +65,14 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
private static final int STATE_ACTIVE = 1;
private static final int STATE_ENDED = 2;
+ /*
+ * When an XA transaction is started, we put the underlying connection
+ * into non-autocommit mode. The old setting is saved in
+ * localAutoCommitMode, so that we can restore it when the XA transaction
+ * ends and the connection returns into local transaction mode.
+ */
+ private boolean localAutoCommitMode = true;
+
private void debug(String s) {
logger.debug("XAResource " + Integer.toHexString(this.hashCode()) + ": " + s);
}
@@ -153,6 +161,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
try
{
+ localAutoCommitMode = conn.getAutoCommit();
conn.setAutoCommit(false);
}
catch (SQLException ex)
@@ -246,7 +255,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
{
stmt.close();
}
- conn.setAutoCommit(true);
+ conn.setAutoCommit(localAutoCommitMode);
return XA_OK;
}
@@ -335,7 +344,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
state = STATE_IDLE;
currentXid = null;
conn.rollback();
- conn.setAutoCommit(true);
+ conn.setAutoCommit(localAutoCommitMode);
}
else
{
@@ -401,7 +410,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
currentXid = null;
conn.commit();
- conn.setAutoCommit(true);
+ conn.setAutoCommit(localAutoCommitMode);
}
catch (SQLException ex)
{
@@ -429,6 +438,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
String s = RecoveredXid.xidToString(xid);
+ localAutoCommitMode = conn.getAutoCommit();
conn.setAutoCommit(true);
Statement stmt = conn.createStatement();
try
@@ -438,6 +448,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
finally
{
stmt.close();
+ conn.setAutoCommit(localAutoCommitMode);
}
}
catch (SQLException ex)
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/libpostgresql-jdbc-java.git
More information about the pkg-java-commits
mailing list