public static void verifyConnectionLeaks(ConnectionFactory connectionFactory) { if (connectionFactory instanceof PooledConnectionFactory) { PooledConnectionFactory pcf = (PooledConnectionFactory) connectionFactory; try { Thread.sleep(500); // C3P0 needs a little delay before reporting the correct number of connections. Bah! assertEquals(pcf.getNumBusyConnectionsAllUsers(), 0); } catch (Exception e) { throw new RuntimeException(e); } } else if (connectionFactory instanceof SimpleConnectionFactory) { SimpleConnectionFactory scf = (SimpleConnectionFactory) connectionFactory; assertEquals(scf.getConnectionCount(), 0); } }
private void log(Connection connection, boolean checkout, boolean before) { if (trace) { String stage = before ? "before" : "after"; String operation = checkout ? "checkout" : "release"; try { log.tracef("DataSource %s %s (NumBusyConnectionsAllUsers) : %d, (NumConnectionsAllUsers) : %d", stage, operation, getNumBusyConnectionsAllUsers(), getNumConnectionsAllUsers()); } catch (SQLException e) { log.sqlFailureUnexpected(e); } if (connection != null) log.tracef("Connection %s : %s", operation, connection); } } }
private void testValuesNoOverrides() throws Exception { storeBuilder = TestCacheManagerFactory .getDefaultCacheConfiguration(false) .persistence() .addStore(JdbcStringBasedStoreConfigurationBuilder.class); factoryBuilder = UnitTestDatabaseManager.configureUniqueConnectionFactory(storeBuilder); ConnectionFactoryConfiguration factoryConfiguration = factoryBuilder.create(); factory.start(factoryConfiguration, Thread.currentThread().getContextClassLoader()); int hardcodedMaxPoolSize = factory.getMaxPoolSize(); Set<Connection> connections = new HashSet<>(); for (int i = 0; i < hardcodedMaxPoolSize; i++) { connections.add(factory.getConnection()); } assert connections.size() == hardcodedMaxPoolSize; assert factory.getNumBusyConnectionsAllUsers() == hardcodedMaxPoolSize; for (Connection conn : connections) { conn.close(); } long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < 2000) { if (factory.getNumBusyConnectionsAllUsers() == 0) break; } //this must happen eventually assert factory.getNumBusyConnectionsAllUsers() == 0; }