mysql链接超时的问题

使用Hibernate + MySQL数据库开发,连接超时问题:java

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

查了一下,原来是mysql超时设置的问题
若是链接闲置8小时 (8小时内没有进行数据库操做), mysql就会自动断开链接, 要重启tomcat.linux

解决办法:sql

    一种. 若是不用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>windows

 四。最很差的解决方案tomcat

使用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属性避免该错误。app

后来查了一些资料,才发现遇到这个问题的人还真很多,大部分都是使用链接池方式时才会出现这个问题,短链接应该很难出现这个问题。这个问题的缘由:

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
相关文章
相关标签/搜索