boolean removeObject = false;
long idleTimeMilis = System.currentTimeMillis() - pair.tstamp;
// 所以: minEvictableIdleTimeMillis改为0;避免无谓的驱逐
if (this.getMinEvictableIdleTimeMillis() > 0L && idleTimeMilis > this.getMinEvictableIdleTimeMillis()) {
removeObject = true;
} else if (this.getSoftMinEvictableIdleTimeMillis() > 0L && idleTimeMilis > this.getSoftMinEvictableIdleTimeMillis() && this.getNumIdle() + 1 > this.getMinIdle()) {
removeObject = true;
}
if (this.getTestWhileIdle() && !removeObject) {
boolean active = false;
try {
this._factory.activateObject(pair.value);
active = true;
} catch (Exception var12) {
removeObject = true;
}
if (active) {
if (!this._factory.validateObject(pair.value)) {
removeObject = true;
} else {
try {
this._factory.passivateObject(pair.value);
} catch (Exception var11) {
removeObject = true;
}
}
}
}
if (removeObject) {
try {
this._factory.destroyObject(pair.value);
} catch (Exception var10) {
}
}
阅读DBCP源码,发现是通过org.apache.commons.pool.impl.GenericObjectPool.Evictor定时任务,按照timeBetweenEvictionRunsMillis配置的周期定时驱逐失效连接,驱逐条件:若连接空闲时间大于"minEvictableIdleTimeMillis",则会驱逐连接,等待垃圾回收。若开启"testWhileIdle"则会执行"validationQuery"。进一步阅读代码,发现执行"validationQuery"后,连接空闲时间并不会重新计算,导致连接在业务低谷时很容易被淘汰,而数据库连接会关联大量对象,创建、回收成本昂贵,并且影响GC。
所以: minEvictableIdleTimeMillis改为0;避免无谓的驱逐; 只要testWhileIdle 为true,且有周期,和validateQuery