本篇文章主要介紹了spring boot如何解決mysql斷連問題,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
在Spring Boot JPA連接Mysql的過程中,經過 8小時后會發現斷連的情況。application.properties配置如下(此坑我跳過,歡迎入坑):
spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring.datasource.driver-class-name=com.mysql.jdbc.Driver
原因分析:
mysql在默認的情況下,如果發現一個連接空閑時間超過8小時,將會在數據庫端自動關閉這個連接。(mysql wait_mysqlout 為8小時)。
解決方式:
1 . Mysql 5 版本之前可以通過在URL后面加入autoReconnect=true,如:
spring.datasource.url=jdbc:mysql://localhost/test?autoReconnect=true
2 . application.properties文件中加入:
spring.datasource.test-on-borrow=false spring.datasource.test-while-idle=true spring.datasource.time-between-eviction-runs-millis=?3600000
3 . 粗暴點的直接修改 wait_timeout 時間:
show?global?variables?like?'wait_timeout';
推薦第二種方式
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END