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
运行结果及报错内容

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配置没有效果
我想要达到的结果
如何才能超时八个小时后还能继续连接
1 搞定时任务 隔几个小时就去操作下数据库试试
看下这篇博客,也许你就懂了,链接:
MySQL插入时间差八小时问题解决报错信息中已经给出提示 将&autoReconnect=true
放到 jdbc连接后边即可避免此问题。如有帮助望采纳!