[libpostgresql-jdbc-java] 03/05: 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:21 UTC 2017


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

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

commit 2f1b5e3285a1a8377cb8a0e06f5e790d416126f3
Author: Kris Jurka <books at ejurka.com>
Date:   Sat May 1 14:41:17 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 993c749..a05e375 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 0270e16..ae9e856 100644
--- a/org/postgresql/xa/PGXAConnection.java
+++ b/org/postgresql/xa/PGXAConnection.java
@@ -63,6 +63,14 @@ public class PGXAConnection extends PooledConnectionImpl 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) {
         Driver.debug("XAResource " + Integer.toHexString(this.hashCode()) + ": " + s);
     }
@@ -150,6 +158,7 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
 
         try
         {
+            localAutoCommitMode = conn.getAutoCommit();
             conn.setAutoCommit(false);
         }
         catch (SQLException ex)
@@ -243,7 +252,7 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
             {
                 stmt.close();
             }
-            conn.setAutoCommit(true);
+            conn.setAutoCommit(localAutoCommitMode);
 
             return XA_OK;
         }
@@ -332,7 +341,7 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
                 state = STATE_IDLE;
                 currentXid = null;
                 conn.rollback();
-                conn.setAutoCommit(true);
+                conn.setAutoCommit(localAutoCommitMode);
             }
             else
             {
@@ -398,7 +407,7 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
             currentXid = null;
 
             conn.commit();
-            conn.setAutoCommit(true);
+            conn.setAutoCommit(localAutoCommitMode);
         }
         catch (SQLException ex)
         {
@@ -426,6 +435,7 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
 
             String s = RecoveredXid.xidToString(xid);
 
+            localAutoCommitMode = conn.getAutoCommit();
             conn.setAutoCommit(true);
             Statement stmt = conn.createStatement();
             try
@@ -435,6 +445,7 @@ public class PGXAConnection extends PooledConnectionImpl 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