db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rhille...@apache.org
Subject svn commit: r1002232 - in /db/derby/code/trunk/java: engine/org/apache/derby/impl/store/access/conglomerate/GenericController.java testing/org/apache/derbyTesting/functionTests/tests/lang/TruncateTableTest.java
Date Tue, 28 Sep 2010 15:56:47 GMT
Author: rhillegas
Date: Tue Sep 28 15:56:47 2010
New Revision: 1002232

URL: http://svn.apache.org/viewvc?rev=1002232&view=rev
Log:
DERBY-268: Eliminate NPE when reading past the end of a cursor on a truncated or dropped table.

Modified:
    db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/conglomerate/GenericController.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/TruncateTableTest.java

Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/conglomerate/GenericController.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/conglomerate/GenericController.java?rev=1002232&r1=1002231&r2=1002232&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/conglomerate/GenericController.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/store/access/conglomerate/GenericController.java
Tue Sep 28 15:56:47 2010
@@ -221,8 +221,16 @@ abstract class GenericController 
         if (container == null)
             open_conglom.reopen();
 
-        open_conglom.getContainer().setEstimatedRowCount(
+        // the container could still be null if a DROP TABLE or TRUNCATE TABLE
+        // has removed the old conglomerate
+
+        container = open_conglom.getContainer();
+
+        if ( container != null )
+        {
+            container.setEstimatedRowCount(
                 count, /* unused flag */ 0);
+        }
     }
 
     /**************************************************************************

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/TruncateTableTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/TruncateTableTest.java?rev=1002232&r1=1002231&r2=1002232&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/TruncateTableTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/TruncateTableTest.java
Tue Sep 28 15:56:47 2010
@@ -224,5 +224,66 @@ public class TruncateTableTest extends B
         aliceConnection.close();
         ruthConnection.close();
     }
+
+    /**
+     * Test that TRUNCATE TABLE and DROP TABLE do not cause held cursors
+     * to trip across an NPE. See DERBY-268.
+     */
+    public void testCursor() throws Exception
+    {
+        Connection cursorConnection = openUserConnection( ALICE );
+        Connection truncatorConnection = openUserConnection( ALICE );
+
+        cursorConnection.setAutoCommit( false );
+        truncatorConnection.setAutoCommit( false );
+
+        cursorMinion( cursorConnection, truncatorConnection, "truncateTab", "truncate table
" );
+        cursorMinion( cursorConnection, truncatorConnection, "dropTab", "drop table " );
+
+        cursorConnection.close();
+    }
+    private void cursorMinion
+        ( Connection cursorConnection, Connection truncatorConnection, String tableName,
String truncationStub )
+        throws Exception
+    {
+        Statement ddlStatement = cursorConnection.createStatement();
+        Statement truncatorStatement = truncatorConnection.createStatement();
+
+        ddlStatement.execute( "create table " + tableName + "( a int )" );
+        ddlStatement.execute( "insert into " + tableName + "( a ) values ( 1 ), ( 2 )" );
+        ddlStatement.close();
+        cursorConnection.commit();
+
+        Statement cursorStatement = cursorConnection.createStatement
+            (
+             ResultSet.TYPE_SCROLL_SENSITIVE,
+             ResultSet.CONCUR_READ_ONLY,
+             ResultSet.HOLD_CURSORS_OVER_COMMIT
+             );
+        ResultSet cursor = cursorStatement.executeQuery( "select * from " + tableName );
+
+        // read first row, then commit the holdable cursor
+        cursor.next();
+        assertEquals( 1, cursor.getInt( 1 ) );
+        cursorConnection.commit();
+
+        // now truncate the table and commit
+        truncatorStatement.execute( truncationStub + tableName );
+        truncatorConnection.commit();
+
+        // we expect to be able to finish draining the cursor
+        cursor.next();
+        assertEquals( 2, cursor.getInt( 1 ) );
+
+        // and we expect to be told that the cursor is drained. this is
+        // where the NPE was raised
+        assertFalse( cursor.next() );
+        
+        cursor.close();
+        cursorConnection.commit();
+        
+        cursorStatement.close();
+        truncatorStatement.close();
+    }
     
 }



Mime
View raw message