4

我使用 ConnectionPool 创建了一个池,如下所示:

我创建了一些任务以从数据库中获取连接并执行它们。我只运行我的应用程序 3 次,我的应用程序抛出异常。

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;
}

但是当我运行很多线程时。它抛出异常

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)
4

2 回答 2

4
 config.maxWait = 10000;
 config.timeBetweenEvictionRunsMillis = 10000;
config.minEvictableIdleTimeMillis = 60000;

这些设置组合起来没有意义。您将只等待 10 秒来获得一个池条目,但您只会每 10 秒驱逐一次它们。驱逐需要比这更频繁地发生。这些默认值分别为 30 秒和 5 秒。

于 2012-08-03T07:25:51.927 回答
2

我检查了我的代码,在某些情况下我错过了 returnObject。谢谢安德烈鲍里索夫

于 2012-08-03T07:34:34.133 回答