天天看點

Hibernate連接配接資料庫逾時設定autoReconnect=true

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 

注意:但是感覺這種方式配置,在mysql5以上好像不生效。

用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>

或者最王道的解決辦法,那就是直接修改mysql的配置檔案my.ini 在配置檔案的最後增加wait_timeout=343232後面的數字是以秒計算。改個10年100年多友善的。

另外的解決辦法:

解決hibernate+mysql出現的隔天連接配接逾時問題

出現錯誤:SQL Error: 0, SQLState: 08S01

Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException

MESSAGE: Software caused connection abort: socket write error

STACKTRACE:

java.net.SocketException: Software caused connection abort: socket write error

** END NESTED EXCEPTION **

Last packet sent to the server was 0 ms ago

問題出現原因

mysql預設為8小時後自動消除空閑連接配接,而hibernate預設空連接配接逾時時間大于這個數。

解決方法

1.找到mysql5.0目錄下的my.ini檔案,在最底處(或任意位置)添加wait_timeout =60(60為自定義值)

2.用c3p0代替hibernate的連接配接池。c3p0.9.1.jar可從hibernate開源項目的lib下面找到,将其拷貝到web-inf/lib下面。在hibernate.cfg.xml配置檔案中添加以下資訊:

<property name="hibernate.c3p0.min_size">2</property>

<property name="hibernate.c3p0.timeout">5000</property>

<property name="hibernate.c3p0.max_statements">100</property>

<property name="hibernate.c3p0.idle_test_period">3000</property>

<property name="hibernate.c3p0.acquire_increment">2</property>

<property name="hibernate.c3p0.validate">false</property>

其中hibernate.c3p0.timeout屬性指定多少秒後連接配接逾時,連接配接池會自動對逾時連接配接進行重查。

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&password=&useUnicode=true&characterEncoding=gbk

&autoReconnect=true&failOverReadOnly=false

其他參數請參見mysql jdbc官方文檔: MySQL Connector/J Documentation若轉載請注明出處!若有疑問,請回複交流!