Spring Boot 配置MySQL數(shù)據(jù)庫(kù)重連的操作方法
使用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來(lái)解決。
需要注意的是:mysql是不推薦使用autoReconnect配置,因?yàn)槿绻麤](méi)有合適處理SQLException的話,它會(huì)帶來(lái)一些數(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ù)庫(kù)連接池庫(kù),支持的連接池包括: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ù)庫(kù)重連的操作方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
出現(xiàn)次數(shù)超過(guò)一半(50%)的數(shù)
給出n個(gè)數(shù),需要我們找出出現(xiàn)次數(shù)超過(guò)一半的數(shù),下面小編給大家分享下我的實(shí)現(xiàn)思路及關(guān)鍵代碼,感興趣的朋友一起學(xué)習(xí)吧2016-07-07
eclipse項(xiàng)目在IDEA中打開并運(yùn)行的詳細(xì)圖文教程
這篇文章主要給大家介紹了關(guān)于eclipse項(xiàng)目在IDEA中打開并運(yùn)行的詳細(xì)圖文教程,至從使用IDEA開發(fā)工具以來(lái),不少次有使用IDEA運(yùn)行Eclipse項(xiàng)目或非Maven項(xiàng)目,所以這里給大家總結(jié)下,需要的朋友可以參考下2023-09-09
使用Java實(shí)現(xiàn)簡(jiǎn)單串口通信
這篇文章主要介紹了使用Java實(shí)現(xiàn)簡(jiǎn)單串口通信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
HttpClient實(shí)現(xiàn)遠(yuǎn)程調(diào)用
這篇文章主要為大家詳細(xì)介紹了HttpClient實(shí)現(xiàn)遠(yuǎn)程調(diào)用的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

