Example usage for org.apache.commons.dbcp2 PoolingDriver registerPool

List of usage examples for org.apache.commons.dbcp2 PoolingDriver registerPool

Introduction

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

Prototype

public synchronized void registerPool(String name, ObjectPool<? extends Connection> pool) 

Source Link

Usage

From source file:JDBCPool.dbcp.demo.offical.PoolingDriverExample.java

/**
 * ?connectURI?PoolingDriver// w w w. j ava  2 s  . c  o m
 * @param connectURI
 * @throws Exception
 */
public static void setupDriver(String connectURI) throws Exception {

    // 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, null);

    // 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...
    //
    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.
    //
}

From source file:PoolingDriverExample.java

public static void setupDriver(String connectURI) throws Exception {
    ////w  w w  .  ja  v  a 2  s.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, null);

    //
    // 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);

    //
    // 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.
    //
}

From source file:PoolingDriverExample.java

public static void setupDriver(String connectURI) throws Exception {
    ///*from www  . j ava2s . c  o  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.
    //
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null);

    //
    // 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...
    //
    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.
    //
}

From source file:org.apache.zeppelin.jdbc.JDBCInterpreter.java

private void createConnectionPool(String url, String user, String propertyKey, Properties properties)
        throws SQLException, ClassNotFoundException {
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, properties);

    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            null);//from ww  w.ja v a2  s .com
    ObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);

    poolableConnectionFactory.setPool(connectionPool);
    Class.forName(properties.getProperty(DRIVER_KEY));
    PoolingDriver driver = new PoolingDriver();
    driver.registerPool(propertyKey + user, connectionPool);
    getJDBCConfiguration(user).saveDBDriverPool(propertyKey, driver);
}

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

public static void initDataSource() {
    try {//w  ww.j  ava  2  s .c o  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   ww  w . ja v a2  s .c om*/
        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.
    //
}