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

使用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数据库重连的操作方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


新闻名称:SpringBoot配置MySQL数据库重连的操作方法
网站地址:http://hxwzsj.com/article/ihisdh.html

其他资讯

Copyright © 2025 青羊区翔捷宏鑫字牌设计制作工作室(个体工商户) All Rights Reserved 蜀ICP备2025123194号-14
友情链接: 企业网站建设 成都企业网站制作 网站设计公司 高端网站设计 成都网站建设 成都网站制作 LED网站设计方案 成都网站建设 企业网站制作 专业网站设计 成都网站建设 定制级高端网站建设 定制网站建设多少钱 营销型网站建设 网站设计 四川成都网站建设 成都定制网站建设 成都网站建设 成都h5网站建设 成都做网站建设公司 手机网站设计 成都网站制作