温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-10-05 23:23:42 来源:脚本之家 阅读:803 作者:zhicc 栏目:编程语言

使用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

总结

以上所述是小编给大家介绍的Spring Boot 配置MySQL数据库重连的操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对亿速云网站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI