Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解決


springboot + mybatis多數據庫 + druid連接池配置成功。正常運行后,訪問第一個數據庫后訪問第二個數據庫,再去訪問第一個數據庫 會報錯:

### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 8,451 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
### The error may exist in com/bwton/dist/dao/sys/DictDao.xml
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select id,code,value,caption,status, create_user,create_time,update_user,update_time from dictionary where code = ? and value <> "!" and status = "1" order by value
### Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 8,451 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
; SQL []; Communications link failure

The last packet successfully received from the server was 8,451 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 8,451 milliseconds ago. The last packet sent successfully to the server was 0 milliseconds ago.
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:98)

 

網上查了下,原因是
MySQL服務器默認的“wait_timeout”是28800秒即8小時(查看mysql實際wait_timeout可以用sql命令:show global variables like ‘wait_timeout’?,意味着如果一個連接的空閑時間超過8個小時,MySQL將自動斷開該連接,而連接池卻認為該連接還是有效的(因為並未校驗連接的有效性),當應用申請使用該連接時,就會導致上面的報錯。

解決方法:

  • 1、修改my.cnf:
    [mysqld]
    wait_timeout=31536000
    interactive_timeout=31536000
    將過期時間修改為1年。
  • 2、在連接URL上添加參數:&autoReconnect=true&failOverReadOnly=false

我的解決采用的是第二種方法,在URL后面加上&autoReconnect=true&failOverReadOnly=false

 

 

網上搜索參考自:

https://blog.csdn.net/a704397849/article/details/93797529

在此也感謝開發老表的協助。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM