解決com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:


com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 126,421,823 milliseconds ago. The last packet sent successfully to the server was 126,421,823 milliseconds ago. is longer than the server configured value of ‘wait_timeout’. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property ‘autoReconnect=true’ to avoid this problem.

 

解決辦法:

修改mysql配置文件,更改連接失效時間

mysql﹥

mysql﹥ show global variables like ‘wait_timeout’;

+—————+———+

| Variable_name | Value |

+—————+———+

| wait_timeout | 28800 |

+—————+———+

1 row in set (0.00 sec)

28800 seconds,也就是8小時。

如果在wait_timeout秒期間內,數據庫連接(java.sql.Connection)一直處於等待狀態,mysql5就將該連接關閉。這時,你的Java應用的連接池仍然合法地持有該連接的引用。當用該連接來進行數據庫操作時,就碰到上述錯誤。這解釋了為什么我的程序第二天不能登錄 的問題。

你可能會想到在tomcat的數據源配置中有沒有辦法解決?的確,在jdbc連接url的配置中,你可以附上“autoReconnect=true”,但這僅對mysql5以前的版本起作用。增加“validation query”似乎也無濟於事。

本人覺得最簡單的辦法,就是對症下葯:既然問題是由mysql5的全局變量wait_timeout的缺省值太小引起的,我們將其改大就好了。

查看mysql5的手冊,發現對wait_timeout的最大值分別是24天/365天(windows/linux)。以windows為 例,假設我們要將其設為21天,我們只要修改mysql5的配置文件“my.ini”(mysql5 installation dir),增加一行:wait_timeout=1814400

需要重新啟動mysql5。

linux系統配置文件:/etc/my.cnf

測試顯示問題解決了。

也可以直接設置

mysql修改wait_timeout

  mysql mysql> show global variables like ‘wait_timeout’;

  其默認值為8小時

  mysql的一個connection空閑時間超過8小時,mysql會自動斷開該連接。

  1.修改配置

  vi /etc/my.cnf

  [mysqld] wait_timeout=10

  # /etc/init.d/mysql restart

  2.直接用sql命令行修改 mysql> set global wait_timeout=604800;

除了wait_timeout,還有一個’interactive_timeout’

同樣可以執行SHOW GLOBAL VARIABLES LIKE ‘interactive_timeout’;來查詢
執行set global interactive_timeout=604800;來設置


免責聲明!

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



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