The last packet successfully received from the server was xxxxx milliseconds ago

文章讨论了在项目中遇到的Druid连接池MySQL数据库时的CommunicationsException错误,分析了配置参数如time-between-eviction-runs-millis和testWhileIdle的影响,以及如何平衡性能与连接的有效性检测。
摘要由CSDN通过智能技术生成
|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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值