/** * Proper closure test * * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws SQLException */ @SuppressWarnings({"unchecked", "rawtypes"}) @Test public void testStatementCacheCheckForProperClosure() 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); field = testClass.getClass().getDeclaredField("logger"); field.setAccessible(true); field.set(null, mockLogger); 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).once().andReturn(false).once(); expect(mockIterator.next()).andReturn(mockStatement).anyTimes(); expect(mockStatement.isClosed()).andReturn(false).once(); mockLogger.error((String) anyObject()); expectLastCall().once(); replay(mockCache, mockStatementCollections, mockIterator, mockStatement, mockLogger); testClass.checkForProperClosure(); verify(mockCache, mockStatement, mockLogger); }
// @Override public void checkForProperClosure() { for (StatementHandle statement : this.cache.values()) { if (!statement.isClosed()) { logger.error( "Statement not closed properly in application\n\n" + statement.getOpenStackTrace()); } } }
// @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(); }