db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1066127 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/ engine/org/apache/derby/impl/jdbc/ testing/org/apache/derbyTesting/functionTests/tests/jdbc4/
Date Tue, 01 Feb 2011 18:37:11 GMT
Author: rhillegas
Date: Tue Feb  1 18:37:11 2011
New Revision: 1066127

URL: http://svn.apache.org/viewvc?rev=1066127&view=rev
Log:
DERBY-4869: Hook up implicitly closed ResultSets to the Statement.closeOnCompletion() logic.

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java
    db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java
    db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java?rev=1066127&r1=1066126&r2=1066127&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/ResultSet.java Tue Feb  1 18:37:11
2011
@@ -460,12 +460,16 @@ public abstract class ResultSet implemen
         if (statement_.openOnClient_ && statement_.isCatalogQuery_) {
             statement_.closeX();
         }
+        
+        nullDataForGC();
+    }
 
+    /** Close Statement if it is set to closeOnCompletion */
+    private void    closeStatementOnCompletion()
+    {
         statement_.closeMeOnCompletion();
         if ( (outerStatement_ != null) && (outerStatement_ != statement_) ) { outerStatement_.closeMeOnCompletion();
}
         outerStatement_ = null;
-
-        nullDataForGC();
     }
 
     public void nullDataForGC() {
@@ -4424,6 +4428,7 @@ public abstract class ResultSet implemen
         if (removeListener) {
             connection_.CommitAndRollbackListeners_.remove(this);
         }
+        closeStatementOnCompletion();
     }
 
     /**

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java?rev=1066127&r1=1066126&r2=1066127&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/Statement.java Tue Feb  1 18:37:11
2011
@@ -1892,6 +1892,16 @@ public class Statement implements java.s
     void markClosed(boolean removeListener) {
         openOnClient_ = false;
         markResultSetsClosed();
+        closeEverythingExceptResultSets( removeListener );
+    }
+
+    /**
+     * Close all resources except for ResultSets. This code was factored out
+     * of markClosed() so that closeMeOnCompletion() could close the
+     * Statement without having to re-close the already closed ResultSets.
+     */
+    private void    closeEverythingExceptResultSets( boolean removeListener )
+    {
         // in case a cursorName was set on the Statement but the Statement was
         // never used to execute a query, the cursorName will not be removed
         // when the resultSets are mark closed, so we need to remove the
@@ -3097,7 +3107,8 @@ public class Statement implements java.s
                 }
 
                 // if we got here, then the Statement has no open ResultSets left
-                close();
+                openOnClient_ = false;
+                closeEverythingExceptResultSets( true );
             }
             catch (SQLException se) {  se.printStackTrace( agent_.getLogWriter() ); }
         }

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java?rev=1066127&r1=1066126&r2=1066127&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedResultSet.java Tue Feb
 1 18:37:11 2011
@@ -615,19 +615,13 @@ public abstract class EmbedResultSet ext
 		     	}
 
 			} finally {
-				isClosed = true;
+				markClosed();
 			    restoreContextStack();
 			}
 
 			// the idea is to release resources, so:
 			currentRow = null;
 
-            // to prevent infinite looping, tell our parent Statement
-            // that we have closed AFTER
-            // we have marked ourself as closed
-            if ( stmt != null) { stmt.closeMeOnCompletion(); }
-            if ( (owningStmt != null) && (owningStmt != stmt) ) { owningStmt.closeMeOnCompletion();
}
-            
 			// we hang on to theResults and messenger
 			// in case more calls come in on this resultSet
 		}
@@ -635,6 +629,23 @@ public abstract class EmbedResultSet ext
 	}
 
     /**
+     * Mark this ResultSet as closed and trigger the closing of the Statement
+     * if necessary.
+     */
+    private void    markClosed()
+    {
+        if ( isClosed ) { return; }
+        
+        isClosed = true;
+
+        // to prevent infinite looping, tell our parent Statement
+        // that we have closed AFTER
+        // we have marked ourself as closed
+        if ( stmt != null) { stmt.closeMeOnCompletion(); }
+        if ( (owningStmt != null) && (owningStmt != stmt) ) { owningStmt.closeMeOnCompletion();
}
+    }
+
+    /**
      * A column may have the value of SQL NULL; wasNull reports whether
      * the last column read had this special value.
      * Note that you must first call getXXX on a column to try to read
@@ -4325,7 +4336,7 @@ public abstract class EmbedResultSet ext
 			// basic cleanup and mark it as closed.
 			if (!isClosed) {
 				closeCurrentStream();
-				isClosed = true;
+                markClosed();
 			}
 
 			throw newSQLException(SQLState.LANG_RESULT_SET_NOT_OPEN, operation);
@@ -4350,7 +4361,7 @@ public abstract class EmbedResultSet ext
             
 		if (appConn.isClosed()) {
             closeCurrentStream();
-            isClosed = true;
+            markClosed();
 			throw Util.noCurrentConnection();
         }
 	}

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java?rev=1066127&r1=1066126&r2=1066127&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/StatementTest.java
Tue Feb  1 18:37:11 2011
@@ -20,6 +20,7 @@
 
 package org.apache.derbyTesting.functionTests.tests.jdbc4;
 
+import org.apache.derbyTesting.functionTests.tests.jdbcapi.Wrapper41Statement;
 import org.apache.derbyTesting.functionTests.tests.jdbcapi.SetQueryTimeoutTest;
 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
@@ -304,6 +305,56 @@ public class StatementTest
     }
 
     /**
+     * Test the closeOnCompletion() and isCloseOnCompletion() methods
+     * when using ResultSets which close implicitly.
+     */
+    public void testCompletionClosure_jdbc4_1_implicitRSClosure() throws Exception
+    {
+        Connection  conn = getConnection();
+        conn.setHoldability( ResultSet.CLOSE_CURSORS_AT_COMMIT );
+        conn.setAutoCommit( true );
+
+        PreparedStatement   ps;
+        ResultSet   rs;
+        Wrapper41Statement  wrapper;
+
+        ps = conn.prepareStatement( "values ( 1 )" );
+        println( "Testing implicit closure WITH autocommit on a " + ps.getClass().getName()
);
+        
+        wrapper = new Wrapper41Statement( ps );
+        wrapper.closeOnCompletion();
+
+        rs = ps.executeQuery();
+        rs.next();
+        rs.next();
+
+        assertTrue( rs.isClosed() );
+        assertTrue( ps.isClosed() );
+
+        conn.setAutoCommit( false );
+
+        // now retry the experiment with an explicit commit
+
+        ps = conn.prepareStatement( "values ( 1 )" );
+        println( "Testing implicit closure WITHOUT autocommit on a " + ps.getClass().getName()
);
+        
+        wrapper = new Wrapper41Statement( ps );
+        wrapper.closeOnCompletion();
+
+        rs = ps.executeQuery();
+        rs.next();
+        rs.next();
+
+        assertFalse( rs.isClosed() );
+        assertFalse( ps.isClosed() );
+
+        conn.commit();
+        
+        assertTrue( rs.isClosed() );
+        assertTrue( ps.isClosed() );
+    }
+
+    /**
      * Create test suite for StatementTest.
      */
     public static Test suite() {



Mime
View raw message