遇到的问题:

        每次查询数据库,druid都会报错,数据还能查出来,但写入会失败

报的错:

com.mysql.cj.jdbc.exceptions.CommunicationsException: The last packet successfully received from the server was 30,053 milliseconds ago. The last packet sent successfully to the server was 30,090 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

Caused by: com.mysql.cj.exceptions.CJCommunicationsException: The last packet successfully received from the server was 30,053 milliseconds ago. The last packet sent successfully to the server was 30,090 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem. 

Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

解决方案:

1.数据库配置连接存活时间太短(一般不会是这个的错)

        查看连接状态:

SHOW VARIABLES LIKE 'wait_timeout';

 

         修改默认连接存活时间:

  1. 打开MySQL配置文件 my.cnf(在Windows上可能是 my.ini)。通常,这个文件位于 MySQL 安装目录的 /etc/ 或 /etc/mysql/ 目录下。
  2. 在配置文件中找到 [mysqld] 部分。
  3. 在 [mysqld] 部分中添加或修改以下行(如果不存在):

wait_timeout = 600

  • 这里的值 600 表示连接的超时时间为 600 秒,您可以根据需要进行调整。

         保存并关闭配置文件,重启 MySQL 服务,以使更改生效。

2.druid配置连接存活时间太长

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    dynamic:
      strict: true
      primary: main
      datasource:
        main:
          url: 
          username: 
          password: 
      druid:
      # 初始化大小,最小,最大
        initial-size: 5
        min-idle: 5
        max-active: 20
        # 配置获取连接等待超时的时间
        max-wait: 60000
        # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        time-between-eviction-runs-millis: 6000
        # 配置一个连接在池中最小生存的时间,单位是毫秒
        min-evictable-idle-time-millis: 200000
        max-evictable-idle-time-millis: 240000
        validation-query: SELECT 1 FROM DUAL
        test-while-idle: true
        test-on-return: true
        # 打开PSCache,并且指定每个连接上PSCache的大小
        pool-prepared-statements: true
        max-pool-prepared-statement-per-connection-size: 20
        # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,wall用于防火墙
        filters: stat,wall,slf4j
  • min-evictable-idle-time-millis: 连接最小存活时间
  • max-evictable-idle-time-millis: 连接最大存活时间

3.数据库版本问题(我是这个问题)

        数据库5.6版本的,有可能与 springboot3 + druid3 兼容有问题,我换了5.7或5.8的数据库都不会报这个错了

GitHub 加速计划 / druid / druid
3
3
下载
阿里云计算平台DataWorks(https://help.aliyun.com/document_detail/137663.html) 团队出品,为监控而生的数据库连接池
最近提交(Master分支:4 个月前 )
9faeba9f - 2 天前
5a1a5f0f - 2 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐