mysql連線逾時的問題

來源:互聯網
上載者:User

標籤:

使用Hibernate + MySQL資料庫開發,連結逾時問題:

com.mysql.jdbc.CommunicationsException: The last packet successfully received from the server was58129 seconds ago.The last packet sent successfully to the server was 58129 seconds ago, which 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逾時設定的問題
如果串連閑置8小時 (8小時內沒有進行資料庫操作), mysql就會自動中斷連線, 要重啟tomcat.

解決辦法:

    一種. 如果不用hibernate的話, 則在 connection url中加參數: autoReconnect=true

jdbc.url=jdbc:mysql://ipaddress:3306/database?autoReconnect=true&autoReconnectForPools=true
    二種。用hibernate的話, 加如下屬性:
        <property name=”connection.autoReconnect”>true</property>
        <property name=”connection.autoReconnectForPools”>true</property>
        <property name=”connection.is-connection-validation-required”>true</property>
    三。要是還用c3p0串連池:
        <property name=”hibernate.c3p0.acquire_increment”>1</property>
        <property name=”hibernate.c3p0.idle_test_period”>0</property>
        <property name=”hibernate.c3p0.timeout”>0</property>
        <property name=”hibernate.c3p0.validate”>true</property>

 四。最不好的解決方案

使用Connector/J串連MySQL資料庫,程式運行較長時間後就會報以下錯誤:

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屬性避免該錯誤。

後來查了一些資料,才發現遇到這個問題的人還真不少,大部分都是使用串連池方式時才會出現這個問題,短串連應該很難出現這個問題。這個問題的原因:

MySQL伺服器預設的“wait_timeout”是28800秒即8小時,意味著如果一個串連的空閑時間超過8個小時,MySQL將自動斷開該串連,而串連池卻認為該串連還是有效(因為並未校正串連的有效性),當應用申請使用該串連時,就會導致上面的報錯。

1. 按照錯誤的提示,可以在JDBC URL中使用autoReconnect屬性,實際測試時使用了autoReconnect=true& failOverReadOnly=false,不過並未起作用,使用的是5.1版本,可能真像網上所說的只對4之前的版本有效。

2.沒辦法,只能修改MySQL的參數了,wait_timeout最大為31536000即1年,在my.cnf中加入:

[mysqld]

wait_timeout=31536000

interactive_timeout=31536000

重啟生效,需要同時修改這兩個參數。

** BEGIN NESTED EXCEPTION **

com.mysql.jdbc.CommunicationsException
MESSAGE: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

Java.io.EOFException

STACKTRACE:

java.io.EOFException
 at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1913)
 at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2304)
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2803)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)

mysql執行逾時設定mysql執行逾時設定
mysql> show variables like ‘%timeout’;
mysql> set wait_timeout = 28800000;
mysql> set interactive_timeout = 28800000; 修改操作如下:開啟/etc/my.cnf,在屬性群組mysqld下面添加參數如下:
[mysqld]
interactive_timeout=28800000
wait_timeout=28800000
windows下在my.ini文中增加:

interactive_timeout=28800000
wait_timeout=28800000

解決MySQL 5資料庫連接逾時問題最近碰到一個mysql5資料庫的問題。就是一個標準的servlet/tomcat網路應用,後台使用mysql資料庫。問題是待機一晚上後,第二天早上第一次登入總是失敗。察看日誌發現如下錯誤:

  “com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure-
  Last packet sent to the server was 0 ms ago.

  經過一番調研,發現很多人都碰到過類似問題,但網上令人滿意的回答並不多。mysql網站上的提問也很多,但並沒有正確答案;百度知道上倒是有一個近似正確的回答。現將本人的解決辦法總結一下
上述問題是由mysql5資料庫的配置引起的。mysql5將其串連的等待時間(wait_timeout)預設為8小時。在其客戶程式中可以這樣來查看其值:

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=18144006
需要重新啟動mysql5。
  linux系統設定檔:/etc/my.cnf
  測試顯示問題解決了。


 

-------------------------------------------

[mysqld]

wait_timeout=2147483
interactive_timeout=2147483  

mysql連線逾時的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.