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这个参数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值