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 da...@apache.org on 2010/08/20 00:13:57 UTC

svn commit: r987331 - in /db/derby/code/trunk/java: client/org/apache/derby/client/net/EncodedInputStream.java testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/StreamTest.java

Author: dag
Date: Thu Aug 19 22:13:57 2010
New Revision: 987331

URL: http://svn.apache.org/viewvc?rev=987331&view=rev
Log:
DERBY-4531 Client setCharacterStream closes its Reader argument stream in finalizer

Patch derby-4531b, which makes the client behave like embedded in this
respect.  

It also adds a new test which shows the clients divergent behavior
prior to the fix in EncodedInputStream. Since the test relies on
explicit gc, it is not guaranteed to show the presence of the bug,
though.


Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/net/EncodedInputStream.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/StreamTest.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/net/EncodedInputStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/net/EncodedInputStream.java?rev=987331&r1=987330&r2=987331&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/net/EncodedInputStream.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/net/EncodedInputStream.java Thu Aug 19 22:13:57 2010
@@ -212,11 +212,6 @@ public final class EncodedInputStream ex
     }
     
     
-    protected void finalize() throws IOException {
-		close();
-    }
-    
-    
     static class PublicBufferOutputStream extends ByteArrayOutputStream{
 	
 		PublicBufferOutputStream(int size){

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/StreamTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/StreamTest.java?rev=987331&r1=987330&r2=987331&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/StreamTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/StreamTest.java Thu Aug 19 22:13:57 2010
@@ -167,4 +167,62 @@ public class StreamTest extends BaseJDBC
     
     private static final String LANG_STREAM_RETRIEVED_ALREADY = "XCL18";
 
+    private boolean didclose = false;
+
+    /**
+     * DERBY-4531: Test that JDBC driver doesn't close a stream handed in to
+     * PreparedStatement.setCharacterStream when the prepared statement is
+     * garbage collected. Prior to thus fix, the client driver did call close
+     * on the stream in its finalizer. After fix to DERBY-4531, both embedded
+     * and client driver leaves the stream open after having read the number of
+     * characters specified.
+     */
+    public void testDerby4531() throws SQLException {
+        setAutoCommit(false);
+
+        Statement s = createStatement();
+        s.executeUpdate("create table tDerby4531(c clob(200))");
+        s.close();
+
+        // Don't use plain prepareStatement, we want ps to be gc'ed below and
+        // BaseJDBCTestCase#prepareStatement saves away a reference to it
+        // thwarting that.
+        PreparedStatement ps = getConnection().
+            prepareStatement("insert into tDerby4531 values (?)");
+        Reader r = new MyLoopingAlphabetReader(200);
+        ps.setCharacterStream(1, r, 200);
+        ps.execute();
+        ps.close();
+        ps = null;
+
+        // Prior to fix for this issue, with client driver, gc of ps causes
+        // close to be called on reader, cf. code in
+        // org.apache.derby.client.net.EncodedInputStream#finalize.
+        System.gc();
+
+        // Sleep so gc thread can do its thing
+        try {
+            Thread.sleep(1000);
+        } catch (Exception e) {
+        }
+
+        synchronized(r) {
+            assertFalse(didclose);
+        }
+
+        rollback();
+    }
+
+    private class MyLoopingAlphabetReader extends LoopingAlphabetReader {
+        public MyLoopingAlphabetReader(int i) {
+            super(i);
+        }
+
+        // Override this so we can detect that it happened.
+        public void close() {
+            synchronized(this) {
+                didclose = true;
+            }
+        }
+    }
 }
\ No newline at end of file