码迷,mamicode.com
首页 > 数据库 > 详细

mysql重连,连接丢失问题:The last packet sent successfully to the server was……

时间:2015-04-12 13:21:45      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

1.log错误日志:

-org.hibernate.exception.JDBCConnectionException: The last packet successfully received from the server was 51,999,860 milliseconds ago. The last packet sent successfully to the server was 51,999,860 milliseconds ago. 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.

其中错误还会提示你修改wait_timeout或是使用Connector/J的autoReconnect属性避免该错误。

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

2.这个问题的原因:

MySQL服务器默认的“wait_timeout”是28800秒即8小时,

意味着如果一个连接的空闲时间超过8个小时,MySQL将自动断开该连接,

而连接池却认为该连接还是有效的(因为并未校验连接的有效性),当应用申请使用该连接时,就会导致上面的报错。

3.解决办法:

a.按照错误的提示,可以在JDBC URL中使用autoReconnect属性,下面是我的配置

<property name="connection.url">
jdbc:mysql://localhost:3306/anxincar?autoReconnect=true
</property> 

其中红色部分为新添加的属性设置
不过网上有另一种说法,有人测试该设置只对mysql4之前的版本有效,具体是否属实还有待考证。

b.我查看了下我们服务器上安装的是mysql5.0,为了确保连接不再丢失而出现此类问题,同时修改MySQL的参数,wait_timeout最大为31536000即1年,在my.ini中加入:

[mysqld]
wait_timeout=31536000
interactive_timeout=31536000

红色部分为新添加的参数设置

最后:重启mysql服务,重启tomcat,让配置文件生效。

 

 

mysql重连,连接丢失问题:The last packet sent successfully to the server was……

标签:

原文地址:http://www.cnblogs.com/WenbinChen/p/4419343.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!