如何使用Spring Boot解決Mysql斷連問題的詳細介紹

黄舟
發布: 2017-03-21 13:28:05
原創
1701 人瀏覽過

這篇文章主要介紹了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_timeout 為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';
登入後複製

#推薦第二種方式

以上是如何使用Spring Boot解決Mysql斷連問題的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!