/** * Tests statement cache clear. * * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testStatementCacheClear() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, SQLException { ConcurrentMap mockCache = createNiceMock(ConcurrentMap.class); List<StatementHandle> mockStatementCollections = createNiceMock(List.class); StatementCache testClass = new StatementCache(1, false, null); Field field = testClass.getClass().getDeclaredField("cache"); field.setAccessible(true); field.set(testClass, mockCache); Iterator<StatementHandle> mockIterator = createNiceMock(Iterator.class); StatementHandle mockStatement = createNiceMock(StatementHandle.class); expect(mockCache.values()).andReturn(mockStatementCollections).anyTimes(); expect(mockStatementCollections.iterator()).andReturn(mockIterator).anyTimes(); expect(mockIterator.hasNext()).andReturn(true).times(2).andReturn(false).once(); expect(mockIterator.next()).andReturn(mockStatement).anyTimes(); mockStatement.close(); expectLastCall().once().andThrow(new SQLException()).once(); mockCache.clear(); expectLastCall().once(); replay(mockCache, mockStatementCollections, mockIterator, mockStatement); testClass.clear(); verify(mockCache, mockStatement); }
// @Override public void clear() { for (StatementHandle statement : this.cache.values()) { try { if (!statement.isClosed()) { statement.close(); } } catch (SQLException e) { // don't log, we might fail if the connection link has died // logger.error("Error closing off statement", e); } } this.cache.clear(); }
// @Override public void clear() { for (StatementHandle statement : this.cache.values()) { try { if (!(statement.isClosed() || statement .isEnqueuedForClosure())) { // this might race with statement release helper but // nothing bad should occur statement.close(); } } catch (SQLException e) { // don't log, we might fail if the connection link has died // logger.error("Error closing off statement", e); } } this.cache.clear(); }