十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
使用jdbc連接MySQL,如果連接失效,可能會(huì)報(bào)類似的錯(cuò)誤:
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.
如錯(cuò)誤提示,可以在連接的url上添加autoReconnect=true來解決。
需要注意的是:mysql是不推薦使用autoReconnect配置,因?yàn)槿绻麤]有合適處理SQLException的話,它會(huì)帶來一些數(shù)據(jù)一致性的副作用,可以參考:https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html中的autoReconect部分。
spring boot 1.4+需要看使用的是什么數(shù)據(jù)庫連接池庫,支持的連接池包括:tomcat, hikari, dbcp(1.5+廢棄), dbcp2。
tomcat
spring.datasource.tomcat.test-on-borrow=true spring.datasource.tomcat.validation-query=SELECT 1