熊哈哈唉 2022-08-02 09:54 采纳率: 80%
浏览 38

mysql连接超过八小时报错

mysql空闲时间超过8小时断开连接

ssm项目配置多数据源,原本使用jndi,自己使用jdbc封装一个工具类,数据驱动使用hutool的DruidFactory

问题相关代码,请勿粘贴截图
public MyDataSource getDateSource() {
        Setting setting = new Setting("classpath:db.setting");
        DataSource dataSource = DruidDSFactory.create(setting).getDataSource();
        return new MyDataSource(dataSource);
    }

db.setting内容

HikariCP配置

自动提交

autoCommit = true

等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException, 缺省:30秒

connectionTimeout = 30000

一个连接idle状态的最大时长(毫秒),超时则被释放(retired),缺省:10分钟

idleTimeout = 600000

一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';)

maxLifetime = 28760000

获取连接前的测试SQL

connectionTestQuery = SELECT 1

最小闲置连接数

minimumIdle = 20

连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)

maximumPoolSize = 200

连接只读数据库时配置为true, 保证安全

readOnly = false

运行结果及报错内容

img


Caused by: com.mysql.cj.exceptions.CJCommunicationsException: The last packet successfully received from the server was 51,144,209 milliseconds ago. The last packet sent successfully to the server was 51,144,224 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.

我的解答思路和尝试过的方法

更改HikariCP配置没有效果

我想要达到的结果

如何才能超时八个小时后还能继续连接

  • 写回答

4条回答 默认 最新

  • 朝与同歌暮同酒 2022-08-02 10:42
    关注

    1 搞定时任务 隔几个小时就去操作下数据库试试

    评论

报告相同问题?

问题事件

  • 创建了问题 8月2日

悬赏问题

  • ¥15 from seleniumwire import webdriver 在抓取http://链接的时候会自动转https://这个怎么解决
  • ¥15 hive直连数据库模式插入mysql表数据失败(相关搜索:数据库)
  • ¥30 不会,学习,有偿解答
  • ¥15 SQL查询语句报错(检查)
  • ¥15 此表中公式应该怎么写
  • ¥15 求HI-TECH PICC 9.50 PL3安装包
  • ¥15 下载ctorch报错,求解
  • ¥15 如何入门学习c语言,单片机
  • ¥15 idea 编辑语言的选择
  • ¥15 Windows下部署Asmjit