mysql 连接池连接超时的问题

来源:互联网 发布:dnf第三方和数据异常 编辑:程序博客网 时间:2024/04/27 14:24

以下内容为转载http://www.blogjava.net/ivanwan/archive/2012/11/06/390893.html

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

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>

 四。最不好的解决方案

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

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

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

重启生效,需要同时修改这两个参数 
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 硫酸铬钾中毒怎么办 门锁能扭动却打不开怎么办 防盗门保险坏了怎么办 厦门工会卡过期怎么办 阴雨天被子受潮怎么办 衣服晾臭了怎么办 喷砂机油水分离泵有油怎么办 水太烫玻璃瓶盖打不开怎么办 玻璃罐的玻璃盖打不开怎么办? 开红酒没有开瓶器怎么办 手机卡突然坏了怎么办 滚筒洗衣机打不开门怎么办 全自动洗衣机门开不开怎么办 好期待评分不足怎么办 单片机数码管亮度低怎么办 猫的同轴灯不亮怎么办 楼下邻居太吵怎么办 冰箱冷却液内露怎么办 冷却水没有了会怎么办 金立m7信号不好怎么办 csgo凉了饰品怎么办 模型拟合度低怎么办 石膏线用发泡胶怎么办 电表上显示err10怎么办 电表显示欠压怎么办 tcl电视遥控器失灵怎么办 玩具汽车遥控器失灵怎么办 玩具车遥控丢了怎么办 按压扣坏了怎么办 电脑用不了鼠标怎么办 多肉上的肉虫子怎么办 警察被取消警衔怎么办 隐形眼镜带歪了怎么办 gta5正在加载慢怎么办 gta5线上卡住了怎么办 皮子掉的颜色怎么办 gta5死了之后车怎么办 潜水口里有水怎么办 800度近视潜水怎么办 不会游泳深潜怎么办 高度近视浮潜怎么办