/** * Test case for cache put. * * @throws SQLException * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ @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); // Calling again should not provide the same object assertNull(cache.get(sql)); // now pretend we have 1 connection being asked for the same statement // twice statement = conn.prepareStatement(sql); Statement statement2 = conn.prepareStatement(sql); statement.close(); // release it again statement2.close(); // release the other one statement2.close(); statement.close(); conn.close(); dsb.shutdown(); CommonTestUtils.logPass(); }
/** * Prepared statement tests. * * @throws SQLException * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ @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); // switch to our mock preparedStatement.set(con, mockCache); expect(mockCache.get(isA(String.class))).andReturn(null); // mockCache.put(isA(String.class), isA(PreparedStatement.class)); 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(); CommonTestUtils.logPass(); }
/** Test for clear statement caches. */ @Test public void testClearStatementCaches() { testClass.statementCachingEnabled = true; mockPreparedStatementCache.clear(); expectLastCall().once(); mockCallableStatementCache.clear(); expectLastCall().once(); replay(mockPreparedStatementCache, mockCallableStatementCache); testClass.clearStatementCaches(true); verify(mockPreparedStatementCache, mockCallableStatementCache); reset(mockPreparedStatementCache, mockCallableStatementCache); mockPool.closeConnectionWatch = true; mockPreparedStatementCache.checkForProperClosure(); expectLastCall().once(); mockCallableStatementCache.checkForProperClosure(); expectLastCall().once(); replay(mockPreparedStatementCache, mockCallableStatementCache); testClass.clearStatementCaches(false); verify(mockPreparedStatementCache, mockCallableStatementCache); }
/** * Mock setup. * * @param cache * @param returnVal * @param params * @param args */ private void doStatementMock( IStatementCache cache, StatementHandle returnVal, Object[] params, Class<?>... args) { expect(cache.get((String) anyObject())).andReturn(returnVal).anyTimes(); // // expect(cache.calculateCacheKey((String)anyObject())).andReturn(testStatementCache.calculateCacheKey((String)params[0])).anyTimes(); if (args.length == 2) { if (params[1].getClass().equals(Integer.class)) { expect(cache.calculateCacheKey((String) anyObject(), anyInt())) .andReturn( testStatementCache.calculateCacheKey((String) params[0], (Integer) params[1])) .anyTimes(); expect(cache.get((String) anyObject(), anyInt())).andReturn(returnVal).anyTimes(); } expect(cache.calculateCacheKey((String) anyObject(), aryEq(new int[] {0, 1}))) .andReturn(testStatementCache.calculateCacheKey((String) params[0], new int[] {0, 1})) .anyTimes(); expect(cache.get((String) anyObject(), aryEq(new int[] {0, 1}))) .andReturn(returnVal) .anyTimes(); expect(cache.calculateCacheKey((String) anyObject(), (String[]) anyObject())) .andReturn( testStatementCache.calculateCacheKey( (String) params[0], new String[] {"test", "bar"})) .anyTimes(); expect(cache.get((String) anyObject(), (String[]) anyObject())) .andReturn(returnVal) .anyTimes(); } if (args.length == 3) { expect(cache.calculateCacheKey((String) anyObject(), anyInt(), anyInt())) .andReturn( testStatementCache.calculateCacheKey( (String) params[0], (Integer) params[1], (Integer) params[2])) .anyTimes(); expect(cache.get((String) anyObject(), anyInt(), anyInt())).andReturn(returnVal).anyTimes(); } if (args.length == 4) { expect(cache.calculateCacheKey((String) anyObject(), anyInt(), anyInt(), anyInt())) .andReturn( testStatementCache.calculateCacheKey( (String) params[0], (Integer) params[1], (Integer) params[2], (Integer) params[3])) .anyTimes(); expect(cache.get((String) anyObject(), anyInt(), anyInt(), anyInt())) .andReturn(returnVal) .anyTimes(); } }