DBCP学习-bug334

https://issues.apache.org/jira/browse/DBCP-334?page=com.atlassian.jira.plugin.ext.subversion%3Asubversion-commits-tabpanel#issue-tabs

在DBCP的bug report上有一个bug:
The BasicDataSource should have the "softMinEvictableIdleTimeMillis" (get and set) as the GenericObjectPool, to avoid destroying idle connections below the "minIdle" value. In this case, connections are dropped and immediately recreated with no reason. The "minEvictableIdleTimeMillis" should not be considered because there are ways to validate idle connections before they are used.

由于DBCP使用commons-pools,pool会有一个Evictor,会定时清理idle connection
startEvictor(_timeBetweenEvictionRunsMillis);
_timeBetweenEvictionRunsMillis表示Evictor执行的时间间隔,Evictor是一个TimerTask

这个bug表示,在从pool种丢弃一个connection链接时,由于要保持minIdle的数目,会立刻再创建一个connection,这样比较浪费资源

解决方法:
commons-pools中有两个参数 minEvictableIdleTimeMillis softMinEvictableIdleTimeMillis
minEvictableIdleTimeMillis 表示一个要被丢弃的话,至少要在pool中空闲了一定的时间,softMinEvictableIdleTimeMillis与minEvictableIdleTimeMillis 是一样的意思,只不过softMinEvictableIdleTimeMillis这个参数有另外一个条件:空闲数大于minIdle

来看一下Evictor的实现:
if ((getMinEvictableIdleTimeMillis() > 0) &&
                    (idleTimeMilis > getMinEvictableIdleTimeMillis())) {
                removeObject = true;
            } else if ((getSoftMinEvictableIdleTimeMillis() > 0) &&
                    (idleTimeMilis > getSoftMinEvictableIdleTimeMillis()) &&
                    ((getNumIdle() + 1)> getMinIdle())) { // +1 accounts for object we are processing
                removeObject = true;
            }
对两个参数使用的唯一区别就在于  ((getNumIdle() + 1)> getMinIdle())) 时参数才有效

所以对于新的版本 建议使用softMinEvictableIdleTimeMillis这个参数


你可能感兴趣的:(DBCP)