DBCP 1.4 BUG 导致连接池爆满解决方案

dubbo 项目用的 commons-dbcp-1.4  和  commons-pool-1.5.4 实现连接池,导致数据库经常爆满,经过调试,发现了个 dbcp的bug:

 

dbcp 源码:

 

BasicDataSource.java

创建datasource的方法:

protected synchronized DataSource createDataSource()
        throws SQLException {
        if (closed) {
            throw new SQLException("Data source is closed");
        }

        // Return the pool if we have already created it
        if (dataSource != null) {
            return (dataSource);
        }

        // create factory which returns raw physical connections
        ConnectionFactory driverConnectionFactory = createConnectionFactory();

        // create a pool for our connections
        createConnectionPool();

        // Set up statement pool, if desired
        GenericKeyedObjectPoolFactory statementPoolFactory = null;
        if (isPoolPreparedStatements()) {
            statementPoolFactory = new GenericKeyedObjectPoolFactory(null,
                        -1, // unlimited maxActive (per key)
                        GenericKeyedObjectPool.WHEN_EXHAUSTED_FAIL,
                        0, // maxWait
                        1, // maxIdle (per key)
                        maxOpenPreparedStatements);
        }

        // Set up the poolable connection factory
        createPoolableConnectionFactory(driverConnectionFactory, statementPoolFactory, abandonedConfig);

        // Create and return the pooling data source to manage the connections
        createDataSourceInstance();
        
        try {
            for (int i = 0 ; i < initialSize ; i++) {
                connectionPool.addObject();
            }
        } catch (Exception e) {
            throw new SQLNestedException("Error preloading the connection pool", e);
        }
        
        return dataSource;
    }

 

该方法先 调用 createConnectionPool() 方法 创建 connectionPool,

接着 调用 createPoolableConnectionFactory() 方法创建 connectionFactory,

最后 再调用 createDataSourceInstance() 方法创建 datasource

 

 

看看 createConnectionPool() 方法实现:

protected void createConnectionPool() {
        // Create an object pool to contain our active connections
        GenericObjectPool gop;
        if ((abandonedConfig != null) && (abandonedConfig.getRemoveAbandoned())) {
            gop = new AbandonedObjectPool(null,abandonedConfig);
        }
        else {
            gop = new GenericObjectPool();
        }
        gop.setMaxActive(maxActive);
        gop.setMaxIdle(maxIdle);
        gop.setMinIdle(minIdle);
        gop.setMaxWait(maxWait);
        gop.setTestOnBorrow(testOnBorrow);
        gop.setTestOnReturn(testOnReturn);
        gop.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        gop.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        gop.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        gop.setTestWhileIdle(testWhileIdle);
        connectionPool = gop;
    }

 

这个方法是:每次调用都会创建一个connectionPool,并且都是直接new 的,

 

问题:

当创建 connectionPool成功,但是创建connectionFactory失败,也就是说createConnectionPool()成功了,但是createPoolableConnectionFactory()出现异常了,则dataSource也创建失败,系统会再次执行 createDataSource() 方法,会再次创建一个新的 coonnectionPool , 我的系统里,一共尝试创建了 15次都失败,最后16次才成功,所以也就创建了16个connectionPool,每一个pool的连接数会至少等于 minIdel 的数量,结果导致数据库连接爆满。。。

 

解决:

 

修改 createConnectionPool() 方法实现,添加非空判断:

    protected void createConnectionPool() {
        //添加非空判断
        if(gop != null){
            return ;
        } 
        // Create an object pool to contain our active connections
        GenericObjectPool gop;
        if ((abandonedConfig != null) && (abandonedConfig.getRemoveAbandoned())) {
            gop = new AbandonedObjectPool(null,abandonedConfig);
        }
        else {
            gop = new GenericObjectPool();
        }
        gop.setMaxActive(maxActive);
        gop.setMaxIdle(maxIdle);
        gop.setMinIdle(minIdle);
        gop.setMaxWait(maxWait);
        gop.setTestOnBorrow(testOnBorrow);
        gop.setTestOnReturn(testOnReturn);
        gop.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        gop.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
        gop.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        gop.setTestWhileIdle(testWhileIdle);
        connectionPool = gop;
    }

 

至此:问题解决。

posted on 2015-05-15 17:22  BennyTian  阅读(5157)  评论(0编辑  收藏  举报