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

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

Introduction

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

Prototype

public void setMaxIdle(int maxIdle) 

Source Link

Document

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

Usage

From source file:org.aludratest.cloud.impl.app.LogDatabase.java

private Connection getConnection() throws SQLException {
    if (dataSource == null) {
        EmbeddedDataSource ds = new EmbeddedDataSource();
        ds.setDatabaseName("acm");
        ConnectionFactory connectionFactory = new DataSourceConnectionFactory(ds);
        PoolableConnectionFactory objFactory = new PoolableConnectionFactory(connectionFactory, null);
        objFactory.setValidationQuery("VALUES 1");
        objFactory.setDefaultAutoCommit(true);
        // max 10 minutes lifetime
        objFactory.setMaxConnLifetimeMillis(1000l * 60 * 10);
        // must be fast, because is local
        objFactory.setValidationQueryTimeout(5);

        GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>(objFactory);
        pool.setMaxIdle(2);
        dataSource = new PoolingDataSource<PoolableConnection>(pool);
    }/*from w w w .ja va2 s . co  m*/

    return dataSource.getConnection();
}