Example usage for org.apache.commons.dbcp2 DriverManagerConnectionFactory DriverManagerConnectionFactory

List of usage examples for org.apache.commons.dbcp2 DriverManagerConnectionFactory DriverManagerConnectionFactory

Introduction

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

Prototype

public DriverManagerConnectionFactory(String connectUri, String uname, String passwd) 

Source Link

Document

Constructor for DriverManagerConnectionFactory.

Usage

From source file:com.github.brandtg.switchboard.MysqlReplicator.java

@Override
public void start() throws Exception {
    // DBCP2 pool
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcString, user, password);
    ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(),
            URLEncoder.encode(jdbcString, ENCODING), "replicatorConnectionPool");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);//from   w  w  w . j  a  v  a 2  s . c o m
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    this.dataSource = new PoolingDataSource<PoolableConnection>(connectionPool);
    LOG.info("Opened connection pool to {} as {}", jdbcString, user);

    // Replication applier
    applier = new MysqlReplicationApplier(inputStream, dataSource);

    super.start();
}

From source file:com.github.brandtg.switchboard.TestMysqlReplicationApplier.java

private PoolingDataSource<PoolableConnection> getDataSource() throws Exception {
    // DBCP2 pool
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbc, "root", "");
    ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(),
            URLEncoder.encode(jdbc, "UTF-8"), "replicatorConnectionPool");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);/*w  ww  . java2  s  .c o  m*/
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    return new PoolingDataSource<PoolableConnection>(connectionPool);
}

From source file:com.zaxxer.hikari.benchmark.BenchBase.java

private void setupDBCP2() {
    org.apache.commons.pool2.impl.GenericObjectPool<org.apache.commons.dbcp2.PoolableConnection> connectionPool;
    DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(jdbcURL, "sa", "");

    // Wrap the connections and statements with pooled variants
    org.apache.commons.dbcp2.PoolableConnectionFactory poolableCF = null;
    poolableCF = new org.apache.commons.dbcp2.PoolableConnectionFactory(connectionFactory, null);

    poolableCF.setValidationQuery("VALUES 1");
    poolableCF.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    poolableCF.setDefaultAutoCommit(false);
    poolableCF.setRollbackOnReturn(true);

    // Create the actual pool of connections, and apply any properties
    connectionPool = new org.apache.commons.pool2.impl.GenericObjectPool(poolableCF);
    connectionPool.setTestOnBorrow(true);
    connectionPool.setMaxIdle(maxPoolSize);

    connectionPool.setMinIdle(MIN_POOL_SIZE);
    connectionPool.setMaxTotal(maxPoolSize);
    connectionPool.setMaxWaitMillis(8000);
    connectionPool.setMinEvictableIdleTimeMillis((int) TimeUnit.MINUTES.toMillis(30));
    poolableCF.setPool(connectionPool);/*from   www. j  ava  2  s  . c o  m*/
    DS = new org.apache.commons.dbcp2.PoolingDataSource(connectionPool);
}

From source file:com.github.brandtg.switchboard.JdbcBasedLogIndex.java

@Override
public void start() throws Exception {
    Class.forName(driverClass);//from   w  ww .  j av  a2  s.com
    LOG.info("Loaded driver {}", driverClass);

    // DBCP2 pool
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionString, user, password);
    ObjectName poolName = new ObjectName(JdbcBasedLogIndex.class.getCanonicalName(),
            URLEncoder.encode(connectionString, ENCODING), "indexConnectionPool");
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            poolName);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<PoolableConnection>(
            poolableConnectionFactory);
    poolableConnectionFactory.setPool(connectionPool);
    this.dataSource = new PoolingDataSource<PoolableConnection>(connectionPool);
    LOG.info("Opened connection pool to {} as {}", connectionString, user);

    createTable();
}

From source file:org.apdplat.superword.tools.MySQLUtils.java

private static DataSource setupDataSource(String connectUri, String uname, String passwd) {
    ////from w  w w  .  j ava 2s  .c om
    // 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.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectUri, uname, passwd);

    //
    // Next we'll create the PoolableConnectionFactory, which wraps
    // the "real" Connections created by the ConnectionFactory with
    // the classes that implement the pooling functionality.
    //
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            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<>(poolableConnectionFactory);

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

    //
    // Finally, we create the PoolingDriver itself,
    // passing in the object pool we created.
    //
    PoolingDataSource<PoolableConnection> dataSource = new PoolingDataSource<>(connectionPool);

    return dataSource;
}

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

public static void initDataSource() {
    try {//from  ww w  .j av  a2  s .  com
        //
        // 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();
    }

}