Communications link failure,The last packet successfully received from
the server was *** millisecond ago.The last packet successfully sent to
the server was *** millisecond ago。
最近做測試,發(fā)現(xiàn)Mysql 過一段時間會無法連接,導(dǎo)致數(shù)據(jù)庫數(shù)據(jù)不一至,極其郁悶。
下面是轉(zhuǎn)一哥門的
使用Connector/J連接MySQL數(shù)據(jù)庫,程序運行較長時間后就會報以下錯誤:
Communications link failure,The last packet successfully received
from the server was *** millisecond ago.The last packet successfully
sent to the server was *** millisecond ago。
其中錯誤還會提示你修改wait_timeout或是使用Connector/J的autoReconnect屬性避免該錯誤。
后來查了一些資料,才發(fā)現(xiàn)遇到這個問題的人還真不少,大部分都是使用連接池方式時才會出現(xiàn)這個問題,短連接應(yīng)該很難出現(xiàn)這個問題。這個問題的原因:
MySQL服務(wù)器默認的“wait_timeout”是28800秒即8小時,意味著如果一個連接的空閑時間超過8個小時,MySQL將自動斷開該連接,而連接池卻認為該連接還是有效的(因為并未校驗連接的有效性),當應(yīng)用申請使用該連接時,就會導(dǎo)致上面的報錯。
1.按照錯誤的提示,可以在JDBC
URL中使用autoReconnect屬性,實際測試時使用了autoReconnect=true&
failOverReadOnly=false,不過并未起作用,使用的是5.1版本,可能真像網(wǎng)上所說的只對4之前的版本有效。
2.沒辦法,只能修改MySQL的參數(shù)了,wait_timeout最大為31536000即1年,在my.cnf中加入:
[mysqld]
wait_timeout=31536000
interactive_timeout=31536000
重啟生效,需要同時修改這兩個參數(shù)。
本文來自CSDN博客,轉(zhuǎn)載請標明出處:http://blog.csdn.net/cau99/archive/2009/12/11/4987760.aspx