Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法
使用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的話,它會帶來一些數(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
dbcp2
spring.datasource.dbcp2.test-on-borrow=true spring.datasource.dbcp2.validation-query=SELECT 1
總結(jié)
以上所述是小編給大家介紹的Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
出現(xiàn)次數(shù)超過一半(50%)的數(shù)
給出n個數(shù),需要我們找出出現(xiàn)次數(shù)超過一半的數(shù),下面小編給大家分享下我的實現(xiàn)思路及關(guān)鍵代碼,感興趣的朋友一起學(xué)習(xí)吧2016-07-07
eclipse項目在IDEA中打開并運(yùn)行的詳細(xì)圖文教程
這篇文章主要給大家介紹了關(guān)于eclipse項目在IDEA中打開并運(yùn)行的詳細(xì)圖文教程,至從使用IDEA開發(fā)工具以來,不少次有使用IDEA運(yùn)行Eclipse項目或非Maven項目,所以這里給大家總結(jié)下,需要的朋友可以參考下2023-09-09
HttpClient實現(xiàn)遠(yuǎn)程調(diào)用
這篇文章主要為大家詳細(xì)介紹了HttpClient實現(xiàn)遠(yuǎn)程調(diào)用的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08

