關於MySQL數據庫緩存連接池超時問題

最近再做一個採用spring-boot2.0+阿里druid+redis+mysql的項目,再部署上線之後,測試過程中遇到如下兩種情況的報錯:

Could not retrieve transaction read-only status from server; nested exception is java.sql.SQLException: Could not retrieve transaction read-only status from server] with root cause

java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.和

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

經過排查發現是因爲MySQL數據庫連接池失效導致的問題。

MySQL默認的數據庫連接池保存時間爲28800秒,即超過8小時就會出現連接池失效,就會導致以上問題。

可以通過以下方式解決這個問題:

1、/etc/my.cnf配置[mysqld]中添加如下 [mysqld] interactive_timeout=2147483 wait_timeout=2147483 interactive_timeout和wait_timeout 在這裏設置的都是秒,另外這個最長時間是24.5天即:2147483秒,設置過大也不能超過這個值,設置好需要對MySQL進行重啓。另外如果項目已經上線爲了避免重啓MySQL數據庫可以通過以下方式修改:

mysql> set global wait_timeout=10;
mysql> show global variables like 'wait_timeout'; 該設置立即生效不用重啓MySQL。

2、修改MySQL中的testOnBorrow爲true,指明是否再從池中取出連接前進行檢驗,如果檢驗失敗,則從池中去除連接並嘗試讀取另一個,這樣顯然會增加一定的開銷,網上一般不建議在生產環境採用這種方法,但是具體影響多少的性能,目前沒有測試結果。

先寫這麼多後面再補。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章