Apache Connection Pool Wait Timeout

I created a pool using ConnectionPool as follows:

I create some taks for getConection from the database and exclusion them. I run my application only 3 times, my application throws an Exception.

PoolableObjectFactory mySqlPoolableObjectFactory = new MySqlPoolableObjectFactory(
            host, dbName, user, password);
    Config config = new GenericObjectPool.Config();
    config.maxActive = 10;
    config.testOnBorrow = true;
    config.testWhileIdle = true;
    config.maxIdle = 5;
    config.minIdle = 1;
    config.maxWait = 10000;
    config.timeBetweenEvictionRunsMillis = 10000;
    config.minEvictableIdleTimeMillis = 60000;

    GenericObjectPoolFactory genericObjectPoolFactory = new GenericObjectPoolFactory(
            mySqlPoolableObjectFactory, config);
    return genericObjectPoolFactory.createPool();

public Connection getConnectionFromPool() {

    Connection conn = null;

    try {

        conn = (Connection) connPool.borrowObject();

    } catch (Exception e) {

        e.printStackTrace();

    }

    return conn;
}

But when I start a lot of threads. He throws Exeption

java.util.NoSuchElementException: Timeout waiting for idle object
    at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1174)
    at vn.vccorp.bigdata.mysql.AdmarketPool.getConnectionFromPool(AdmarketPool.java:76)
+5
source share
2 answers
 config.maxWait = 10000;
 config.timeBetweenEvictionRunsMillis = 10000;
config.minEvictableIdleTimeMillis = 60000;

These settings do not make sense in combination. You will only wait 10 seconds for the combined recording, but you will evict them every 10 seconds. Evictions should occur much more often than this. By default, they are 30 seconds and 5 seconds, respectively.

+4
source

I checked my code, in some cases I missed returnObject. Thanks Andrey Borisov

+2

All Articles