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

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

Introduction

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

Prototype

public synchronized ObjectPool<? extends Connection> getConnectionPool(String name) throws SQLException 

Source Link

Usage

From source file:PoolingDriverExample.java

public static void printDriverStats() throws Exception {
    PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
    ObjectPool<? extends Connection> connectionPool = driver.getConnectionPool("example");

    System.out.println("NumActive: " + connectionPool.getNumActive());
    System.out.println("NumIdle: " + connectionPool.getNumIdle());
}

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

public Connection GetPoolConnection() throws Exception {

    Connection conn = null;//from w ww . ja  v a  2  s . co m

    try {
        long startTime = System.currentTimeMillis();
        conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:" + this.EXTERNAL_SERVICE.name());
        PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:");
        ObjectPool<? extends Connection> connectionPool = driver
                .getConnectionPool(this.EXTERNAL_SERVICE.name());
        logger.log(Level.FINE,
                "NumActive: " + connectionPool.getNumActive() + " NumIdle: " + connectionPool.getNumIdle());
        //System.out.println("NumActive: " + connectionPool.getNumActive() + "  NumIdle: " + connectionPool.getNumIdle());
        if (connectionPool.getNumActive() == config.getMaxActive()) {
            config.setMaxActive(config.getMaxActive() + 50);
            try {
                shutdownDriver();
            } catch (Exception e) {
                logger.log(Level.FINE, e.getMessage());
            }
        }

        long endTime = System.currentTimeMillis();
        //System.out.println("Total connection time: " + this.EXTERNAL_SERVICE.name() + " "  + (endTime - startTime) );
        logger.log(Level.FINE, "Open connection for " + this.EXTERNAL_SERVICE.name());
    } catch (SQLException e) {
        //System.out.println("Create connection failed. " + e.getMessage());
        logger.log(Level.SEVERE, "Create connection failed. ", e);
        throw new Exception(e);
    }
    return conn;
}