You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by km...@apache.org on 2010/06/23 00:04:57 UTC

svn commit: r957053 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/ testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ testing/org/apache/derbyTesting/junit/

Author: kmarsden
Date: Tue Jun 22 22:04:56 2010
New Revision: 957053

URL: http://svn.apache.org/viewvc?rev=957053&view=rev
Log:
DERBY-4653  Avoid unnecessary round-trip for commit in the client driver

Contributed by Lily Wei lilywei at yahoo dot com


Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Connection.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalConnection.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/J2EEDataSourceTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Connection.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Connection.java?rev=957053&r1=957052&r2=957053&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Connection.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Connection.java Tue Jun 22 22:04:56 2010
@@ -570,11 +570,10 @@ public abstract class Connection impleme
         // But note that rollback() is less harmless, rollback() shouldn't be used in auto-commit mode.
         // This behavior is subject to further review.
 
-        //   if (!this.inUnitOfWork)
-        //     return;
-        // We won't try to be "too smart", if the user requests a commit, we'll flow a commit,
-        // regardless of whether or not we're in a unit of work or in auto-commit mode.
-        //
+        //DERBY-4653
+        //If we are not in a transaction, we don't want to flow commit. We just return.
+        if (!this.inUnitOfWork_)
+             return;       
         if (isXAConnection_) {
             agent_.beginWriteChainOutsideUOW();
             writeCommit();

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalConnection.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalConnection.java?rev=957053&r1=957052&r2=957053&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalConnection.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalConnection.java Tue Jun 22 22:04:56 2010
@@ -577,6 +577,20 @@ public class LogicalConnection implement
 		}
     }
 
+    /**
+     * Returns the client-side transaction id from am.Connection. 
+     * <p>
+     * <em>NOTE:</em> This method was added for testing purposes. Avoid use in
+     * production code if possible.
+     **/
+    public int getTransactionID() {
+        if (physicalConnection_ == null) {
+            return -1;
+        } else {
+            return physicalConnection_.getTransactionID();
+        }
+    }
+    
     //----------------------------------------------------------------------------
 
     public int getServerVersion() {

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/J2EEDataSourceTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/J2EEDataSourceTest.java?rev=957053&r1=957052&r2=957053&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/J2EEDataSourceTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/J2EEDataSourceTest.java Tue Jun 22 22:04:56 2010
@@ -173,6 +173,7 @@ public class J2EEDataSourceTest extends 
                 "testClientTraceFileDSConnectionAttribute"));
         suite.addTest(new J2EEDataSourceTest(
                 "testClientMessageTextConnectionAttribute"));
+        suite.addTest(new J2EEDataSourceTest("testConnectionFlowCommit"));
         return suite;
     }
     
@@ -2067,6 +2068,64 @@ public class J2EEDataSourceTest extends 
         pc.close();
     }
     /**
+     * check whether commit without statement will flow by checking its transaction id
+     * on client. This test is run only for client where commits without an
+     * active transactions will not flow to the server.
+     * DERBY-4653
+     * 
+     * @throws SQLException
+     **/
+    public void testConnectionFlowCommit()
+            throws SQLException {
+        ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
+
+        PooledConnection pc = ds.getPooledConnection();
+        Connection conn = pc.getConnection();
+
+        testConnectionFlowCommitWork(conn, 1);
+        conn.close();
+        
+        //Test for XADataSource
+        XADataSource xs = J2EEDataSource.getXADataSource();
+        XAConnection xc = xs.getXAConnection();
+        conn = xc.getConnection();
+        testConnectionFlowCommitWork(conn, 1);
+        conn.close();
+        
+        //Test for DataSource
+        DataSource jds = JDBCDataSource.getDataSource();
+        conn = jds.getConnection();
+        testConnectionFlowCommitWork(conn, 1);
+        conn.close();       
+    }
+
+    /**
+     * Doing the work for test Connection.flowcommit() and Connection.flowrollback()
+     * @param conn
+     * @throws SQLException
+     **/  
+    private void testConnectionFlowCommitWork(Connection conn, int expectednumtransaction) throws SQLException {
+        //DERBY 4653 - make sure commit with no work does not flow in client
+        int startXactId = getClientTransactionID(conn);
+        Statement s = conn.createStatement();
+        ResultSet rs = s.executeQuery("values 1");
+        rs.next();
+        assertEquals(1, rs.getInt(1));
+        rs.close();
+        conn.commit();
+        // second commit should not flow
+        conn.commit();
+        int endXactId = getClientTransactionID(conn);
+        
+        //to verify the fix for DERBY-4653. Only one transaction
+        assertTrue("Should have had 1 xact, startXactId = "
+                    + startXactId + " endXactId = " + endXactId ,
+                    ((endXactId - startXactId) == 1) );
+        
+        s.close();
+    }
+    
+    /**
      * Check setTransactioIsolation and with four connection in connection pool
      * for DERBY-4343 case
      * 
@@ -2106,6 +2165,7 @@ public class J2EEDataSourceTest extends 
         conn.close();
     
     }
+    
     // test that an xastart in auto commit mode commits the existing work.
     // test fix of a bug ('beetle 5178') wherein XAresource.start() when 
     // auto-commit is true did not implictly commit any transaction

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java?rev=957053&r1=957052&r2=957053&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java Tue Jun 22 22:04:56 2010
@@ -1303,6 +1303,29 @@ public abstract class BaseJDBCTestCase
      
     }
   
+    /**
+     * Attempts to obtain the client-side transaction counter from the given
+     * connection, which is internal state information.
+     * <p>
+     * <em>NOTE:</em> Use with care, accesses internal state.
+     *
+     * @param conn the connection
+     * @return Internal client transaction id.
+     * @throws SQLException if the given connection is an embedded connection,
+     *      or if invoking the required method fails
+     **/
+    public static int getClientTransactionID(Connection conn)
+            throws SQLException {
+        try {
+            Method m = conn.getClass().getMethod(
+                    "getTransactionID", new Class[] {});
+            return ((Integer) m.invoke(conn, new Object[] {} )).intValue();
+        } catch (Exception e) {
+            SQLException se = new SQLException(e.getMessage());
+            se.initCause(e);
+            throw se;
+        }
+    }
 
     /**
      * Return estimated row count for runtime statistics.