Example usage for org.apache.commons.dbcp2 PoolableConnectionFactory getPool

List of usage examples for org.apache.commons.dbcp2 PoolableConnectionFactory getPool

Introduction

In this page you can find the example usage for org.apache.commons.dbcp2 PoolableConnectionFactory getPool.

Prototype

public synchronized ObjectPool<PoolableConnection> getPool() 

Source Link

Document

Returns the ObjectPool in which Connection s are pooled.

Usage

From source file:eu.peppol.persistence.jdbc.OxalisDataSourceFactoryDbcpImplTest.java

/**
 * Verifies that we can create a pooled jdbc data source using the JDBC .jar-file supplied in the global configuration
 * file./* w  w w . j  av  a  2 s  .co m*/
 *
 * @throws Exception
 */
@Test
public void testLoadJdbcDriverUsingCustomClassLoader() throws Exception {
    ConnectionFactory driverConnectionFactory = createConnectionFactory(false);

    ObjectName poolName = new ObjectName("no.difi.oxalis", "connectionPool", "TestPool");
    PoolableConnectionFactory factory = new PoolableConnectionFactory(driverConnectionFactory, poolName);

    GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<>(factory);
    factory.setPool(pool);

    pool.setMaxTotal(10);
    pool.setMaxWaitMillis(100);

    assertEquals(pool.getFactory(), factory);

    PoolableConnectionFactory pcf = (PoolableConnectionFactory) ((GenericObjectPool<?>) pool).getFactory();
    ObjectPool<PoolableConnection> pool1 = pcf.getPool();

    PoolingDataSource<PoolableConnection> poolingDataSource = new PoolingDataSource<>(pool);

    Connection connection = poolingDataSource.getConnection();
    assertNotNull(connection);

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("select current_date()");

    assertTrue(resultSet.next());
}