Search in sources :

Example 66 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project pgjdbc by pgjdbc.

the class PGDataSourceFactoryTest method testCreateConnectionPoolDataSourceConfigured.

@Test
public void testCreateConnectionPoolDataSourceConfigured() throws Exception {
    Properties properties = new Properties();
    properties.put(DataSourceFactory.JDBC_DATABASE_NAME, "db");
    ConnectionPoolDataSource dataSource = dataSourceFactory.createConnectionPoolDataSource(properties);
    Assert.assertNotNull(dataSource);
    Assert.assertTrue(dataSource instanceof ConnectionPool);
    ConnectionPool connectionPoolDataSource = (ConnectionPool) dataSource;
    Assert.assertEquals("db", connectionPoolDataSource.getDatabaseName());
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) ConnectionPool(org.postgresql.jdbc2.optional.ConnectionPool) Properties(java.util.Properties) Test(org.junit.Test)

Example 67 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project commons-dbcp by apache.

the class SharedPoolDataSource method registerPool.

private void registerPool(final String userName, final String password) throws NamingException, SQLException {
    final ConnectionPoolDataSource cpds = testCPDS(userName, password);
    // Create an object pool to contain our PooledConnections
    factory = new KeyedCPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeoutDuration(), isRollbackAfterValidation());
    factory.setMaxConn(getMaxConnDuration());
    final GenericKeyedObjectPoolConfig<PooledConnectionAndInfo> config = new GenericKeyedObjectPoolConfig<>();
    config.setBlockWhenExhausted(getDefaultBlockWhenExhausted());
    config.setEvictionPolicyClassName(getDefaultEvictionPolicyClassName());
    config.setLifo(getDefaultLifo());
    config.setMaxIdlePerKey(getDefaultMaxIdle());
    config.setMaxTotal(getMaxTotal());
    config.setMaxTotalPerKey(getDefaultMaxTotal());
    config.setMaxWait(getDefaultMaxWait());
    config.setMinEvictableIdleTime(getDefaultMinEvictableIdleDuration());
    config.setMinIdlePerKey(getDefaultMinIdle());
    config.setNumTestsPerEvictionRun(getDefaultNumTestsPerEvictionRun());
    config.setSoftMinEvictableIdleTime(getDefaultSoftMinEvictableIdleDuration());
    config.setTestOnCreate(getDefaultTestOnCreate());
    config.setTestOnBorrow(getDefaultTestOnBorrow());
    config.setTestOnReturn(getDefaultTestOnReturn());
    config.setTestWhileIdle(getDefaultTestWhileIdle());
    config.setTimeBetweenEvictionRuns(getDefaultDurationBetweenEvictionRuns());
    final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> tmpPool = new GenericKeyedObjectPool<>(factory, config);
    factory.setPool(tmpPool);
    pool = tmpPool;
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) GenericKeyedObjectPoolConfig(org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig) GenericKeyedObjectPool(org.apache.commons.pool2.impl.GenericKeyedObjectPool)

Example 68 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project sqlite-jdbc by xerial.

the class SQLiteConnectionPoolDataSourceTest method proxyConnectionCloseTest.

@Disabled
@Test
public void proxyConnectionCloseTest() throws SQLException {
    ConnectionPoolDataSource ds = new SQLiteConnectionPoolDataSource();
    PooledConnection pooledConn = ds.getPooledConnection();
    System.out.println("pooledConn: " + pooledConn.getClass());
    Connection handle = pooledConn.getConnection();
    System.out.println("pooledConn.getConnection: " + handle.getClass());
    Statement st = handle.createStatement();
    System.out.println("statement: " + st.getClass());
    Connection stConn = handle.createStatement().getConnection();
    System.out.println("statement connection:" + stConn.getClass());
    // This closes the physical connection, not the proxy
    stConn.close();
    Connection handle2 = pooledConn.getConnection();
}
Also used : SQLiteConnectionPoolDataSource(org.sqlite.javax.SQLiteConnectionPoolDataSource) ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) SQLiteConnectionPoolDataSource(org.sqlite.javax.SQLiteConnectionPoolDataSource) Statement(java.sql.Statement) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 69 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project sqlite-jdbc by xerial.

the class SQLiteConnectionPoolDataSourceTest method connectionTest.

@Test
public void connectionTest() throws SQLException {
    ConnectionPoolDataSource ds = new SQLiteConnectionPoolDataSource();
    PooledConnection pooledConn = ds.getPooledConnection();
    Connection handle = pooledConn.getConnection();
    assertFalse(handle.isClosed());
    assertTrue(handle.createStatement().execute("select 1"));
    Connection handle2 = pooledConn.getConnection();
    assertTrue(handle.isClosed());
    try {
        handle.createStatement().execute("select 1");
        fail();
    } catch (SQLException e) {
        assertEquals("Connection is closed", e.getMessage());
    }
    assertTrue(handle2.createStatement().execute("select 1"));
    handle2.close();
    handle = pooledConn.getConnection();
    assertTrue(handle.createStatement().execute("select 1"));
    pooledConn.close();
    assertTrue(handle.isClosed());
}
Also used : SQLiteConnectionPoolDataSource(org.sqlite.javax.SQLiteConnectionPoolDataSource) ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) PooledConnection(javax.sql.PooledConnection) SQLiteConnectionPoolDataSource(org.sqlite.javax.SQLiteConnectionPoolDataSource) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PooledConnection(javax.sql.PooledConnection) Test(org.junit.jupiter.api.Test)

Example 70 with ConnectionPoolDataSource

use of javax.sql.ConnectionPoolDataSource in project bboss by bbossgroups.

the class PerUserPoolDataSource method registerPool.

private synchronized void registerPool(String username, String password) throws javax.naming.NamingException, SQLException {
    ConnectionPoolDataSource cpds = testCPDS(username, password);
    Integer userMax = getPerUserMaxActive(username);
    int maxActive = (userMax == null) ? getDefaultMaxActive() : userMax.intValue();
    userMax = getPerUserMaxIdle(username);
    int maxIdle = (userMax == null) ? getDefaultMaxIdle() : userMax.intValue();
    userMax = getPerUserMaxWait(username);
    int maxWait = (userMax == null) ? getDefaultMaxWait() : userMax.intValue();
    // Create an object pool to contain our PooledConnections
    GenericObjectPool pool = new GenericObjectPool(null);
    pool.setMaxActive(maxActive);
    pool.setMaxIdle(maxIdle);
    pool.setMaxWait(maxWait);
    pool.setWhenExhaustedAction(whenExhaustedAction(maxActive, maxWait));
    pool.setTestOnBorrow(getTestOnBorrow());
    pool.setTestOnReturn(getTestOnReturn());
    pool.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
    pool.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
    pool.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
    pool.setTestWhileIdle(getTestWhileIdle());
    // Set up the factory we will use (passing the pool associates
    // the factory with the pool, so we do not have to do so
    // explicitly)
    CPDSConnectionFactory factory = new CPDSConnectionFactory(cpds, pool, getValidationQuery(), isRollbackAfterValidation(), username, password);
    Object old = managers.put(getPoolKey(username, password), factory);
    if (old != null) {
        throw new IllegalStateException("Pool already contains an entry for this user/password: " + username);
    }
}
Also used : ConnectionPoolDataSource(javax.sql.ConnectionPoolDataSource) GenericObjectPool(com.frameworkset.commons.pool.impl.GenericObjectPool)

Aggregations

ConnectionPoolDataSource (javax.sql.ConnectionPoolDataSource)71 PooledConnection (javax.sql.PooledConnection)33 SQLException (java.sql.SQLException)28 Connection (java.sql.Connection)19 XAConnection (javax.sql.XAConnection)15 XADataSource (javax.sql.XADataSource)12 Statement (java.sql.Statement)8 CallableStatement (java.sql.CallableStatement)7 DataSource (javax.sql.DataSource)7 PreparedStatement (java.sql.PreparedStatement)6 J2EEDataSource (org.apache.derbyTesting.junit.J2EEDataSource)6 JDBCDataSource (org.apache.derbyTesting.junit.JDBCDataSource)6 ResultSet (java.sql.ResultSet)5 Context (javax.naming.Context)5 InitialContext (javax.naming.InitialContext)5 Test (org.junit.Test)3 Test (org.junit.jupiter.api.Test)3 Savepoint (java.sql.Savepoint)2 Properties (java.util.Properties)2 SQLiteConnectionPoolDataSource (org.sqlite.javax.SQLiteConnectionPoolDataSource)2