datasource.setJdbcInterceptors(
TestStatementCacheInterceptor.class.getName()
+ "(prepared=true,callable=false,max=1);"
+ StatementCounterInterceptor.class.getName());
Connection con = datasource.getConnection();
StatementCounterInterceptor counter = findInterceptor(con, StatementCounterInterceptor.class);
PreparedStatement ps1, ps2;
ps1 = con.prepareStatement("select 1");
Assert.assertEquals(1, counter.getActiveCount());
ps1.close();
Assert.assertEquals("Statement goes into cache, not closed", 1, counter.getActiveCount());
ps1 = con.prepareStatement("select 1");
Assert.assertEquals("Reusing statement from cache", 1, counter.getActiveCount());
ps2 = con.prepareStatement("select 1");
Assert.assertEquals("Reusing statement from cache", 2, counter.getActiveCount());
ps2.close();
Assert.assertEquals("Statement goes into cache, not closed", 2, counter.getActiveCount());
ps1.close();
// Cache has "max=1". The following tests BZ 54732.
Assert.assertEquals("Statement does not go into cache, closed", 1, counter.getActiveCount());
con.close();
Assert.assertEquals("Connection returned to the pool. Statement is in cache", 1, counter.getActiveCount());
datasource.close();
Assert.assertEquals("Pool cleared. All statements in cache are closed", 0, counter.getActiveCount());
}