[libpostgresql-jdbc-java] 15/22: 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:53 UTC 2017


This is an automated email from the git hooks/post-receive script.

ebourg-guest pushed a commit to tag REL8_4_702
in repository libpostgresql-jdbc-java.

commit 482c77d67efdcaf2b7db16c96bba20ea34bc294c
Author: Kris Jurka <books at ejurka.com>
Date:   Sat May 1 14:40:57 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 16bc46e..a53b543 100644
--- a/org/postgresql/xa/PGXAConnection.java
+++ b/org/postgresql/xa/PGXAConnection.java
@@ -64,6 +64,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);
     }
@@ -152,6 +160,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
 
         try
         {
+            localAutoCommitMode = conn.getAutoCommit();
             conn.setAutoCommit(false);
         }
         catch (SQLException ex)
@@ -245,7 +254,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
             {
                 stmt.close();
             }
-            conn.setAutoCommit(true);
+            conn.setAutoCommit(localAutoCommitMode);
 
             return XA_OK;
         }
@@ -334,7 +343,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
                 state = STATE_IDLE;
                 currentXid = null;
                 conn.rollback();
-                conn.setAutoCommit(true);
+                conn.setAutoCommit(localAutoCommitMode);
             }
             else
             {
@@ -400,7 +409,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
             currentXid = null;
 
             conn.commit();
-            conn.setAutoCommit(true);
+            conn.setAutoCommit(localAutoCommitMode);
         }
         catch (SQLException ex)
         {
@@ -428,6 +437,7 @@ public class PGXAConnection extends PGPooledConnection implements XAConnection,
 
             String s = RecoveredXid.xidToString(xid);
 
+            localAutoCommitMode = conn.getAutoCommit();
             conn.setAutoCommit(true);
             Statement stmt = conn.createStatement();
             try
@@ -437,6 +447,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