当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot 配置MySQL数据库重连的操作方法

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

北瓜是什么,小神仙电脑乐园,技校门下载

使用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的话,它会带来一些数据一致性的副作用,可以参考:中的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数据库重连的操作方法,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网