欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Spring Boot 配置MySQL数据库重连的操作方法

时间:2021-10-06 09:08:32|栏目:JAVA代码|点击:

使用jdbc连接MySQL,如果连接失效,可能会报类似的错误:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 84,371,623 milliseconds ago.

The last packet sent successfully to the server was 78,860,631 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.

如错误提示,可以在连接的url上添加autoReconnect=true来解决。

需要注意的是:mysql是不推荐使用autoReconnect配置,因为如果没有合适处理SQLException的话,它会带来一些数据一致性的副作用,可以参考:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html中的autoReconect部分。

spring boot 1.4+需要看使用的是什么数据库连接池库,支持的连接池包括:tomcat, hikari, dbcp(1.5+废弃), dbcp2。

tomcat

spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.validation-query=SELECT 1

dbcp2

spring.datasource.dbcp2.test-on-borrow=true
spring.datasource.dbcp2.validation-query=SELECT 1

总结

上一篇:详解Java面向对象编程中方法的使用

栏    目:JAVA代码

下一篇:详解SpringSecurity中的Authentication信息与登录流程

本文标题:Spring Boot 配置MySQL数据库重连的操作方法

本文地址:http://www.codeinn.net/misctech/178909.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有