Example usage for org.apache.commons.pool.impl GenericObjectPool setMaxIdle

List of usage examples for org.apache.commons.pool.impl GenericObjectPool setMaxIdle

Introduction

In this page you can find the example usage for org.apache.commons.pool.impl GenericObjectPool setMaxIdle.

Prototype

public synchronized void setMaxIdle(int maxIdle) 

Source Link

Document

Sets the cap on the number of "idle" instances in the pool.

Usage

From source file:us.daveread.basicquery.BasicQuery.java

/**
 * Configures the database connection pool and sets the pool configuration.
 * /*from   ww  w  .  j av  a2 s .  co m*/
 * @param connectionPool
 *          The ObjectPool
 * @param connectURI
 *          The url specifying the database to which it
 *          connects using JDBC
 * @param pUserId
 *          The user id attribute
 * @param pPassword
 *          The password attribute
 * 
 * @throws java.lang.Exception
 *           Throws an SQL exception that provides
 *           information on a database access error
 *           or other errors
 * 
 * @todo Make the pool access dynamic (use the dynamic class loader?)
 *       so that the application will compile/run without the Apache
 *       commons library.
 */
private void configurePool(GenericObjectPool connectionPool, String connectURI, String pUserId,
        String pPassword) throws Exception {
    String lowerCaseConnectURI;
    String validationQuery;

    lowerCaseConnectURI = connectURI.toLowerCase();
    validationQuery = null;

    if (lowerCaseConnectURI.startsWith("jdbc:sybase")) {
        validationQuery = "select getdate()";
    } else if (lowerCaseConnectURI.startsWith("jdbc:mysql")) {
        validationQuery = "select 1";
    } else if (lowerCaseConnectURI.startsWith("jdbc:oracle")) {
        validationQuery = "select sysdate from dual";
    } else if (lowerCaseConnectURI.startsWith("jdbc:mssql")) {
        validationQuery = "select 1";
    }

    // Pool settings - someday a dialog and persistence should be
    // added to put these under user control
    connectionPool.setMaxActive(1);
    connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK);
    connectionPool.setMaxWait(CONN_POOL_MAX_WAIT_MS);
    connectionPool.setMaxIdle(CONN_POOL_MAX_IDLE_CONNECTIONS);
    connectionPool.setTimeBetweenEvictionRunsMillis(CONN_POOL_TIME_BETWEEN_EVICT_RUNS_MS);
    connectionPool.setNumTestsPerEvictionRun(CONN_POOL_NUM_TESTS_PER_EVICT_RUN);
    connectionPool.setMinEvictableIdleTimeMillis(CONN_POOL_EVICT_IDLE_TIME_MS);

    final DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,
            pUserId, pPassword);
    final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
            connectionPool, null, null, false, true);

    if (validationQuery != null) {
        connectionPool.setTestOnBorrow(true);
        connectionPool.setTestWhileIdle(true);
        poolableConnectionFactory.setValidationQuery(validationQuery);
    }
}