|2024-04-08 15:01:43.912|http-nio-80-exec-81|trace_id=64684ebdaf7585c81b5c3e302303cd5e|ERROR|com.alibaba.druid.pool.DruidPooledStatement|errorCheck|370|CommunicationsException, druid version 1.2.20, jdbcUrl : jdbc:mysql://192.188.200.200:4000/data_uc_sit_0001?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai&tinyInt1isBit=false, testWhileIdle true, idle millis 42977, minIdle 10, poolingCount 8, timeBetweenEvictionRunsMillis 60000, lastValidIdleMillis 42977, driver com.mysql.cj.jdbc.Driver, exceptionSorter com.alibaba.druid.pool.vendor.MySqlExceptionSorter
|2024-04-08 15:01:43.928|http-nio-80-exec-122|trace_id=6ae06d8d93c6ead4e4448a39327d46e7|ERROR|com.alibaba.druid.pool.DruidDataSource|handleFatalError|1988|{conn-59242} discard
|com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet successfully received from the server was 42,975 milliseconds ago. The last packet sent successfully to the server was 42,976 milliseconds ago.
项目上经常碰到这种问题,特此记录
Druid配置如下
spring:
#配置数据库信息
datasource:
dynamic:
p6spy: false # 默认false,建议线上关闭。
druid:
# 初始连接数
initial-size: 10
# 最小连接池数量
min-idle: 10
# 最大连接池数量
max-active: 10
# 配置获取连接等待超时的时间
max-wait: 60000
# 配置间隔多久进行一次检测,检测需要关闭的空闲连接,单位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存时间,单位是毫秒
min-evictable-idle-time-millis: 30000
# 配置一个连接在池中最大生存时间,单位是毫秒
max-evictable-idle-time-millis: 90000
# 配置检测连接是否有效
validation-query: select 1
报错中说明上次成功时间是42975ms之前。配置文件中配置的time-between-eviction-runs-millis是60000ms,报错日志中说明testWhileIdle为true。testOnBorrow和testOnReturn默认都为false。testOnBorrow可以避免这个错,但是太占用性能,一般生产环境不能打开。
testWhileIdle为true:代表拿到连接时如果连接空闲时间大于time-between-eviction-runs-millis则进行一次有效性判断。
min-evictable-idle-time-millis配置为30000ms,也就是说连接可能空闲30000ms就关闭了,42975ms也是很有可能关闭的。但是拿到连接后,空闲60000ms(time-between-eviction-runs-millis)才会进行一次空闲检测。理论上time-between-eviction-runs-millis)应当小于等于min-evictable-idle-time-millis