使用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=truespring.datasource.tomcat.validation-query=SELECT 1
dbcp2
spring.datasource.dbcp2.test-on-borrow=truespring.datasource.dbcp2.validation-query=SELECT 1
總結
以上所述是小編給大家介紹的Spring Boot 配置MySQL數據庫重連的操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!
新聞熱點
疑難解答
圖片精選