StatementCache implements IStatementCache { public StatementHandle get(String key){ StatementHandle statement = this.cache.get(key); if (statement != null && !statement.logicallyClosed.compareAndSet(true, false)){ statement = null; } if (this.maintainStats){ if (statement != null){ this.statistics.incrementCacheHits(); } else { this.statistics.incrementCacheMiss(); } } return statement; } StatementCache(int size, boolean maintainStats, Statistics statistics); String calculateCacheKey(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability); String calculateCacheKey(String sql, int resultSetType, int resultSetConcurrency); String calculateCacheKey(String sql, int autoGeneratedKeys); String calculateCacheKey(String sql, int[] columnIndexes); String calculateCacheKey(String sql, String[] columnNames); StatementHandle get(String key); StatementHandle get(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability); StatementHandle get(String sql, int resultSetType, int resultSetConcurrency); StatementHandle get(String sql, int autoGeneratedKeys); StatementHandle get(String sql, int[] columnIndexes); StatementHandle get(String sql, String[] columnNames); int size(); void clear(); void checkForProperClosure(); void putIfAbsent(String key, StatementHandle handle);  }
@Test public void testPreparedStatement() throws SQLException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ BoneCP dsb = null ; CommonTestUtils.logTestInfo("Tests that prepared statements are obtained from cache when set."); config.setMinConnectionsPerPartition(10); config.setMaxConnectionsPerPartition(20); config.setAcquireIncrement(5); config.setPartitionCount(1); config.setStatementsCacheSize(1); config.setLogStatementsEnabled(true); config.setStatementReleaseHelperThreads(0); dsb = new BoneCP(config); ConnectionHandle con = (ConnectionHandle) dsb.getConnection(); Field preparedStatement = con.getClass().getDeclaredField("preparedStatementCache"); preparedStatement.setAccessible(true); preparedStatement.set(con, mockCache); expect(mockCache.get(isA(String.class))).andReturn(null); replay(mockCache); Statement statement = con.prepareStatement(CommonTestUtils.TEST_QUERY); statement.close(); verify(mockCache); reset(mockCache); expect(mockCache.get(isA(String.class))).andReturn(null); replay(mockCache); con.prepareStatement(CommonTestUtils.TEST_QUERY); statement.close(); verify(mockCache); dsb.shutdown(); statement.close(); con.close(); dsb.close(); CommonTestUtils.logPass(); }
@Test public void testStatementCacheNotInCache() throws SQLException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ CommonTestUtils.logTestInfo("Tests statement not in cache."); StatementCache cache = new StatementCache(5, false, null); assertNull(cache.get("nonExistent")); CommonTestUtils.logPass(); }
@Test public void testStatementCachePut() throws SQLException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ CommonTestUtils.logTestInfo("Tests statement close (put in cache)."); String sql = CommonTestUtils.TEST_QUERY; BoneCP dsb = null ; config.setMinConnectionsPerPartition(1); config.setMaxConnectionsPerPartition(5); config.setAcquireIncrement(1); config.setPartitionCount(1); config.setStatementsCacheSize(5); config.setStatementReleaseHelperThreads(0); config.setStatisticsEnabled(true); dsb = new BoneCP(config); Connection conn = dsb.getConnection(); Statement statement = conn.prepareStatement(sql); statement.close(); Field statementCache = conn.getClass().getDeclaredField("preparedStatementCache"); statementCache.setAccessible(true); IStatementCache cache = (IStatementCache) statementCache.get(conn); statement = cache.get(sql); assertNotNull(statement); assertNull(cache.get(sql)); statement = conn.prepareStatement(sql); Statement statement2 = conn.prepareStatement(sql); statement.close(); statement2.close(); statement2.close(); statement.close(); conn.close(); dsb.shutdown(); dsb.close(); CommonTestUtils.logPass(); }