連接數據庫超時設置autoReconnect=true


mysql經典的8小時問題

鏈自:

http://blog.csdn.net/bluesnail216/article/details/15810119

參考http://www.wang1314.com/doc/topic-1520183-1.html

  http://www.jb51.net/article/32284.htm

  http://blog.csdn.net/wanghai__/article/details/8075762

  https://www.oschina.net/question/1245614_133952

1,問題現象:

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. 

解決辦法: 
如果連接閑置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>

 以上轉載鏈接地址:http://blog.aqsc.cn/article.php?type=blog&itemid=1016

2,另外:關於Mysql鏈接參數的說明如下:

 

mysql JDBC Driver

常用的有兩個,一個是gjt(Giant Java Tree)組織提供的mysql驅動,其JDBC Driver名稱(Java類名)為:org.gjt.mm.mysql.Driver

詳情請參見網站:http://www.gjt.org/

或在本網站下載mysql JDBC Driver(mm.jar)

另一個是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&amp;password=&amp;useUnicode=true&amp;characterEncoding=gbk

&amp;autoReconnect=true&amp;failOverReadOnly=false

其他參數請參見mysql jdbc官方文檔: MySQL Connector/J Documentation


免責聲明!

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



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