@Override public void start(ConnectionFactoryConfiguration config, ClassLoader classLoader) throws PersistenceException { if (!started) { sharedFactory = new PooledConnectionFactory(); sharedFactory.start(config, classLoader); started = true; } }
@Override public Connection getConnection() throws PersistenceException { return sharedFactory.getConnection(); }
private void logAfter(Connection connection, boolean checkout) { log(connection, checkout, false); }
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; }
public void testConnectionLeakGuessDialect() throws Exception { JdbcStringBasedStoreConfigurationBuilder storeBuilder = TestCacheManagerFactory .getDefaultCacheConfiguration(false) .persistence() .addStore(JdbcStringBasedStoreConfigurationBuilder.class); UnitTestDatabaseManager.buildTableManipulation(storeBuilder.table()); PooledConnectionFactory connectionFactory = new PooledConnectionFactory(); ConnectionFactoryConfiguration config = UnitTestDatabaseManager .configureUniqueConnectionFactory(storeBuilder).create(); connectionFactory.start(config, Thread.currentThread().getContextClassLoader()); // JdbcStringBasedStoreConfiguration defaults to null dialect, so dialect and versions must be guessed TableManager tableManager = TableManagerFactory.getManager(connectionFactory, storeBuilder.create()); tableManager.setCacheName("GuessDialect"); tableManager.start(); UnitTestDatabaseManager.verifyConnectionLeaks(connectionFactory); tableManager.stop(); connectionFactory.stop(); }
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); } } }
@BeforeMethod public void beforeMethod() { factory = new PooledConnectionFactory(); }
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 testConnectionPoolLoaded(Class connectionPoolType) throws Exception { storeBuilder = TestCacheManagerFactory .getDefaultCacheConfiguration(false) .persistence() .addStore(JdbcStringBasedStoreConfigurationBuilder.class); factoryBuilder = UnitTestDatabaseManager.configureUniqueConnectionFactory(storeBuilder); ConnectionFactoryConfiguration factoryConfiguration = factoryBuilder.create(); factory.start(factoryConfiguration, Thread.currentThread().getContextClassLoader()); Field field = factory.getClass().getDeclaredField("connectionPool"); field.setAccessible(true); ConnectionPool connectionPool = (ConnectionPool) field.get(factory); assert connectionPool != null; assert connectionPoolType.isInstance(connectionPool); } }
@Override public void releaseConnection(Connection conn) { sharedFactory.releaseConnection(conn); } }
@Override public Connection getConnection() throws PersistenceException { try { logBefore(true); Connection connection = connectionPool.getConnection(); logAfter(connection, true); return connection; } catch (SQLException e) { throw new PersistenceException("Failed obtaining connection from PooledDataSource", e); } }
public JdbcStoreReader(StoreProperties props) { this.props = props; this.marshaller = SerializationConfigUtil.getMarshaller(props); this.config = JdbcConfigurationUtil.getStoreConfig(props); this.connectionFactory = new PooledConnectionFactory(); String segmentCount = props.get(Element.SEGMENT_COUNT); this.metaData = new DbMetaData(config.dialect(), config.dbMajorVersion(), config.dbMinorVersion(), false, false, // If we don't have segments then disable it segmentCount == null || Integer.parseInt(segmentCount) <= 0); this.stringConfig = config.table(); this.binaryConfig = createBinaryTableConfig(); connectionFactory.start(config.connectionFactory(), JdbcStoreReader.class.getClassLoader()); }
private void testNoDriverClassFound() throws Exception { storeBuilder = TestCacheManagerFactory .getDefaultCacheConfiguration(false) .persistence() .addStore(JdbcStringBasedStoreConfigurationBuilder.class); factoryBuilder = UnitTestDatabaseManager.configureBrokenConnectionFactory(storeBuilder); ConnectionFactoryConfiguration factoryConfiguration = factoryBuilder.create(); factory.start(factoryConfiguration, Thread.currentThread().getContextClassLoader()); }
private void logBefore(boolean checkout) { log(null, checkout, true); }