psteitz commented on a change in pull request #42:
URL: https://github.com/apache/commons-dbcp/pull/42#discussion_r449928885



##########
File path: 
src/test/java/org/apache/commons/dbcp2/TestPStmtPoolingBasicDataSource.java
##########
@@ -233,6 +233,52 @@ public void testPStmtPoolingAcrossClose() throws Exception 
{
         assertSame(inner1, inner2);
     }
 
+    @Test
+    public void testPStmtPoolingAcrossCloseWithClearOnReturn() throws 
Exception {
+        ds.setMaxTotal(1); // only one connection in pool needed
+        ds.setMaxIdle(1);
+        ds.setClearStatementPoolOnReturn(true);
+        ds.setAccessToUnderlyingConnectionAllowed(true);
+        final Connection conn1 = getConnection();
+        assertNotNull(conn1);
+        assertEquals(1, ds.getNumActive());
+        assertEquals(0, ds.getNumIdle());
+
+        final PreparedStatement stmt1 = conn1.prepareStatement("select 'a' 
from dual");
+        assertNotNull(stmt1);
+        final Statement inner1 = ((DelegatingPreparedStatement) 
stmt1).getInnermostDelegate();
+        assertNotNull(inner1);
+        stmt1.close();
+        
+        final PreparedStatement stmt2 = conn1.prepareStatement("select 'a' 
from dual");
+        assertNotNull(stmt2);
+        final Statement inner2 = ((DelegatingPreparedStatement) 
stmt2).getInnermostDelegate();
+        assertNotNull(inner2);
+        assertSame(inner1, inner2); // from the same connection the statement 
must be pooled
+        stmt2.close();
+        
+        conn1.close();
+        assertTrue(inner1.isClosed());
+        assertTrue(inner2.isClosed());
+
+        assertEquals(0, ds.getNumActive());
+        assertEquals(1, ds.getNumIdle());
+
+        final Connection conn2 = getConnection();
+        assertNotNull(conn2);
+        assertEquals(1, ds.getNumActive());
+        assertEquals(0, ds.getNumIdle());
+
+        final PreparedStatement stmt3 = conn2.prepareStatement("select 'a' 
from dual");
+        assertNotNull(stmt3);
+        final Statement inner3 = ((DelegatingPreparedStatement) 
stmt3).getInnermostDelegate();
+        assertNotNull(inner3);
+
+        assertNotSame(inner1, inner3); // when aquiring the connection the 
next time, statement must be new
+        

Review comment:
       Nice job finding a way to test if the pool is cleared.  I would be +1 to 
exposing a property on PoolingConnection that returns the current size of the 
statement pool or even a getter for the pool itself (like BDS does).  Client 
code (and tests) would have to cast to PoolingConnection to get it, but I could 
see how it might be useful.  With that added, we could also beef up statement 
pooling tests like this without reflection hacks.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to