1,問題現象:php
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
用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>
sql
以上轉載連接地址:http://blog.aqsc.cn/article.php?type=blog&itemid=1016數據庫
2,另外:關於Mysql連接參數的說明以下:tomcat
mysql JDBC Driver服務器
經常使用的有兩個,一個是gjt(Giant Java Tree)組織提供的mysql驅動,其JDBC Driver名稱(JAVA類名)爲:org.gjt.mm.mysql.Driverapp
詳情請參見網站:http://www.gjt.org/socket
或在本網站下載mysql JDBC Driver(mm.jar)ide
另外一個是mysql官方提供的JDBC Driver,其JAVA類名爲:com.mysql.jdbc.Driver網站
驅動下載網址:http://dev.mysql.com/downloads/,進入其中的MySQL Connector/J區域下載。
mysql JDBC URL格式以下:
jdbc:mysql://[host:port],[host:port].../[database][?參數名1][=參數值1][&參數名2][=參數值2]...
現只列舉幾個重要的參數,以下表所示:
參數名稱 | 參數說明 | 缺省值 | 最低版本要求 |
user | 數據庫用戶名(用於鏈接數據庫) | 全部版本 | |
password | 用戶密碼(用於鏈接數據庫) | 全部版本 | |
useUnicode | 是否使用Unicode字符集,若是參數characterEncoding設置爲gb2312或gbk,本參數值必須設置爲true | false | 1.1g |
characterEncoding | 當useUnicode設置爲true時,指定字符編碼。好比可設置爲gb2312或gbk | false | 1.1g |
autoReconnect | 當數據庫鏈接異常中斷時,是否自動從新鏈接? | false | 1.1 |
autoReconnectForPools | 是否使用針對數據庫鏈接池的重連策略 | false | 3.1.3 |
failOverReadOnly | 自動重連成功後,鏈接是否設置爲只讀? | true | 3.0.12 |
maxReconnects | autoReconnect設置爲true時,重試鏈接的次數 | 3 | 1.1 |
initialTimeout | autoReconnect設置爲true時,兩次重連之間的時間間隔,單位:秒 | 2 | 1.1 |
connectTimeout | 和數據庫服務器創建socket鏈接時的超時,單位:毫秒。 0表示永不超時,適用於JDK 1.4及更高版本 | 0 | 3.0.1 |
socketTimeout | socket操做(讀寫)超時,單位:毫秒。 0表示永不超時 | 0 | 3.0.1 |
對應中文環境,一般mysql鏈接URL能夠設置爲:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk&autoReconnect=true&failOverReadOnly=false
在使用數據庫鏈接池的狀況下,最好設置以下兩個參數:
autoReconnect=true&failOverReadOnly=false
須要注意的是,在xml配置文件中,url中的&符號須要轉義成&。好比在tomcat的server.xml中配置數據庫鏈接池時,mysql jdbc url樣例以下:
jdbc:mysql://localhost:3306/test?user=root&password=&useUnicode=true&characterEncoding=gbk
&autoReconnect=true&failOverReadOnly=false
其餘參數請參見mysql jdbc官方文檔: MySQL Connector/J Documentation