背景:由于数据库上的连接设定为空闲30分钟后关闭,那么客户端这边就需要在这此前回收掉,不然获取连接的时候会出问题
配置:
initialSize=1
minIdle=1
maxActive=20
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
phyTimeoutMillis=1500000
validationQuery=SELECT 'x'
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
removeAbandoned=true
removeAbandonedTimeout=1800
logAbandoned=true
在应用运行过程中会出现异常:
{conn-110017, stmt-120323} execute error. SELECT 'x'
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet successfully received from the server was 2,256,880 milliseconds ago. The last packet sent successfully to the server was 928,132 milliseconds ago.
经过排查是因为超时而导致连接关闭了,然后发现DruidDataSource里面,如果配置了phyTimeoutMillis,那么基本上可以排除掉,但是由于下面这段代码:DruidDataSoufce.shrink
if (idleMillis < minEvictableIdleTimeMillis) {
break;
}
会导致没有遍历完池的所有连接,会遗留有超时连接在里面。