[libpostgresql-jdbc-java] 02/13: A XAConnections default autocommit state should be true. The driver was previously setting autocommit to false and assuming it would stay that way. This caused two problems. First, some applications expected to be able to issue local autocommit transactions prior to begin(). Second, some TMs (Geronimo) set the autocommit state to true themselves, which causes problems as the driver did not expect it to be changed. This patch correctly disables and enables autocommit around begin, prepare, commit, and rollback methods.

Emmanuel Bourg ebourg-guest at moszumanska.debian.org
Mon Jan 9 10:20:03 UTC 2017


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

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

commit b937f44c931e678a36f023f4d181f6ede43cd407
Author: Kris Jurka <books at ejurka.com>
Date:   Wed Apr 25 19:33:09 2007 +0000

    A XAConnections default autocommit state should be true.  The
    driver was previously setting autocommit to false and assuming it
    would stay that way.  This caused two problems.  First, some
    applications expected to be able to issue local autocommit
    transactions prior to begin().  Second, some TMs (Geronimo) set the
    autocommit state to true themselves, which causes problems as the
    driver did not expect it to be changed.  This patch correctly disables
    and enables autocommit around begin, prepare, commit, and rollback
    methods.
    
    Allan Saddi
---
 org/postgresql/test/xa/XADataSourceTest.java | 31 +++++++++++++++++++++
 org/postgresql/xa/PGXAConnection.java        | 41 +++++++++++++---------------
 2 files changed, 50 insertions(+), 22 deletions(-)

diff --git a/org/postgresql/test/xa/XADataSourceTest.java b/org/postgresql/test/xa/XADataSourceTest.java
index 45a4632..2978207 100644
--- a/org/postgresql/test/xa/XADataSourceTest.java
+++ b/org/postgresql/test/xa/XADataSourceTest.java
@@ -217,6 +217,37 @@ public class XADataSourceTest extends TestCase {
         xaRes.rollback(xid);
     }
 
+    public void testAutoCommit() throws Exception {
+        Xid xid = new CustomXid(6);
+
+        assertTrue(conn.getAutoCommit());
+
+        xaRes.start(xid, XAResource.TMNOFLAGS);
+        assertFalse(conn.getAutoCommit());
+        xaRes.end(xid, XAResource.TMSUCCESS);
+        assertFalse(conn.getAutoCommit());
+        xaRes.commit(xid, true);
+        assertTrue(conn.getAutoCommit());
+
+        xaRes.start(xid, XAResource.TMNOFLAGS);
+        xaRes.end(xid, XAResource.TMSUCCESS);
+        xaRes.prepare(xid);
+        assertTrue(conn.getAutoCommit());
+        xaRes.commit(xid, false);
+        assertTrue(conn.getAutoCommit());
+
+        xaRes.start(xid, XAResource.TMNOFLAGS);
+        xaRes.end(xid, XAResource.TMSUCCESS);
+        xaRes.rollback(xid);
+        assertTrue(conn.getAutoCommit());
+
+        xaRes.start(xid, XAResource.TMNOFLAGS);
+        xaRes.end(xid, XAResource.TMSUCCESS);
+        xaRes.prepare(xid);
+        xaRes.rollback(xid);
+        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 77ca9d0..a0f8c41 100644
--- a/org/postgresql/xa/PGXAConnection.java
+++ b/org/postgresql/xa/PGXAConnection.java
@@ -48,9 +48,8 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
 
     PGXAConnection(BaseConnection conn) throws SQLException
     {
-        super(conn, false, true);
+        super(conn, true, true);
         this.conn = conn;
-        this.conn.setAutoCommit(false);
         this.state = STATE_IDLE;
     }
 
@@ -104,6 +103,15 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
         if (state == STATE_ENDED)
             throw new PGXAException(GT.tr("Transaction interleaving not implemented"), XAException.XAER_RMERR);
 
+        try
+        {
+            conn.setAutoCommit(false);
+        }
+        catch (SQLException ex)
+        {
+            throw new PGXAException(GT.tr("Error disabling autocommit"), ex, XAException.XAER_RMERR);
+        }
+
         // Preconditions are met, Associate connection with the transaction
         state = STATE_ACTIVE;
         currentXid = xid;
@@ -190,6 +198,7 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
             {
                 stmt.close();
             }
+            conn.setAutoCommit(true);
 
             return XA_OK;
         }
@@ -278,27 +287,21 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
                 state = STATE_IDLE;
                 currentXid = null;
                 conn.rollback();
+                conn.setAutoCommit(true);
             }
             else
             {
                 String s = RecoveredXid.xidToString(xid);
 
                 conn.setAutoCommit(true);
+                Statement stmt = conn.createStatement();
                 try
                 {
-                    Statement stmt = conn.createStatement();
-                    try
-                    {
-                        stmt.executeUpdate("ROLLBACK PREPARED '" + s + "'");
-                    }
-                    finally
-                    {
-                        stmt.close();
-                    }
+                    stmt.executeUpdate("ROLLBACK PREPARED '" + s + "'");
                 }
                 finally
                 {
-                    conn.setAutoCommit(false);
+                    stmt.close();
                 }
             }
         }
@@ -350,6 +353,7 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
             currentXid = null;
 
             conn.commit();
+            conn.setAutoCommit(true);
         }
         catch (SQLException ex)
         {
@@ -378,21 +382,14 @@ public class PGXAConnection extends PooledConnectionImpl implements XAConnection
             String s = RecoveredXid.xidToString(xid);
 
             conn.setAutoCommit(true);
+            Statement stmt = conn.createStatement();
             try
             {
-                Statement stmt = conn.createStatement();
-                try
-                {
-                    stmt.executeUpdate("COMMIT PREPARED '" + s + "'");
-                }
-                finally
-                {
-                    stmt.close();
-                }
+                stmt.executeUpdate("COMMIT PREPARED '" + s + "'");
             }
             finally
             {
-                conn.setAutoCommit(false);
+                stmt.close();
             }
         }
         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