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

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

Introduction

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

Prototype

public synchronized void setPool(ObjectPool<PoolableConnection> pool) 

Source Link

Document

Sets the ObjectPool in which to pool Connection s.

Usage

From source file:org.springframework.boot.jta.narayana.DbcpXADataSourceWrapper.java

@Override
public DataSource wrapDataSource(XADataSource xaDataSource) throws Exception {
    DataSourceXAResourceRecoveryHelper helper = new DataSourceXAResourceRecoveryHelper(xaDataSource);
    recoveryManager.registerXAResourceRecoveryHelper(helper);
    System.out.println("register xa recovery helper " + helper);

    DataSourceXAConnectionFactory dataSourceXAConnectionFactory = new DataSourceXAConnectionFactory(tm,
            xaDataSource);//from w w  w.j  a  v a 2 s. c  om
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
            dataSourceXAConnectionFactory, null);
    GenericObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    return new ManagedDataSource<>(connectionPool, dataSourceXAConnectionFactory.getTransactionRegistry());

}

From source file:science.freeabyss.hulk.jdbc.dbcp.PoolingDriverExample.java

public static void initDataSource() {
    try {/*from w w w .j  a  v  a  2s.co  m*/
        //
        // First, we'll create a ConnectionFactory that the
        // pool will use to create Connections.
        // We'll use the DriverManagerConnectionFactory,
        // using the connect string passed in the command line
        // arguments.
        //
        PropertiesUtil.init("dbcp.properties");
        Class.forName(PropertiesUtil.getString("dbcp.driver"));

        ConnectionFactory connFactory = new DriverManagerConnectionFactory(PropertiesUtil.getString("dbcp.url"),
                PropertiesUtil.getString("dbcp.username"), PropertiesUtil.getString("dbcp.password"));

        //
        // Next, we'll create the PoolableConnectionFactory, which wraps
        // the "real" Connections created by the ConnectionFactory with
        // the classes that implement the pooling functionality.
        //
        PoolableConnectionFactory poolFactory = new PoolableConnectionFactory(connFactory, null);

        //
        // Now we'll need a ObjectPool that serves as the
        // actual pool of connections.
        //
        // We'll use a GenericObjectPool instance, although
        // any ObjectPool implementation will suffice.
        //
        ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolFactory);

        // Set the factory's pool property to the owning pool
        poolFactory.setPool(connectionPool);

        //
        // Finally, we create the PoolingDriver itself...
        //
        Class.forName("org.apache.commons.dbcp2.PoolingDriver");
        PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");

        //
        // ...and register our pool with it.
        //
        driver.registerPool("example", connectionPool);

        //
        // Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
        // to access our pool of Connections.
        //
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}

From source file:za.co.wilderness.WildernessPoolingDriver.java

private void setupDriver() throws Exception {

    String jdbcDriverName = "com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource";

    try {/*from   w  w  w . j  a  v  a  2 s . c o m*/
        java.lang.Class.forName(jdbcDriverName).newInstance();
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
        //System.out.println("Error when attempting to obtain DB Driver: " + jdbcDriverName + " on "+ new Date().toString() + e.getMessage());
        logger.log(Level.SEVERE,
                "Error when attempting to obtain DB Driver: " + jdbcDriverName + " on " + new Date().toString(),
                e);
        throw new Exception(e);
    }

    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(this.connectURI, this.properties);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);

    GenericObjectPoolConfig genConfig = new GenericObjectPoolConfig();
    genConfig.setMaxIdle(this.config.getMaxIdle());
    genConfig.setMaxTotal(this.config.getMaxActive());
    genConfig.setMinIdle(this.config.getMinIdle());
    genConfig.setMaxWaitMillis(this.config.getMaxWaitMillis());
    genConfig.setTimeBetweenEvictionRunsMillis(5000);
    genConfig.setTestWhileIdle(true);

    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory,
            genConfig);

    // Set the factory's pool property to the owning pool
    poolableConnectionFactory.setPool(connectionPool);

    //
    // Finally, we create the PoolingDriver itself...
    //
    Class.forName("org.apache.commons.dbcp2.PoolingDriver");
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    //System.out.println("Driver : " + driver.toString());
    logger.log(Level.FINE, "Driver : " + driver.toString());

    //
    // ...and register our pool with it.
    //
    driver.registerPool(EXTERNAL_SERVICE.name(), connectionPool);

    //
    // Now we can just use the connect string "jdbc:apache:commons:dbcp:example"
    // to access our pool of Connections.
    //
}