MySQL连接超时处理
1.由于MySQL默认是8小时的wait_timeout,当超过8小时的连接时间后,在JAVA中调用将出现如下报错
SEVERE EXCEPTION com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was175588 seconds ago.The last packet sent successfully to the server was 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.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:)
at java.lang.reflect.Constructor.newInstance(Constructor.java:)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:)
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:)
at cn.sm.ApplePack.ApplePack.handle(ApplePack.java:)
at cn.sm.DataProcessNSQConsumerApp.handleMessage(DataProcessNSQConsumerApp.java:)
at com.sproutsocial.nsq.SubConnection$.run(SubConnection.java:)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:)
at java.lang.Thread.run(Thread.java:)
Caused by: java.net.SocketException: 断开的管道 (Write failed)
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:)
at java.net.SocketOutputStream.write(SocketOutputStream.java:)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:)
... more
解决方法有两个:修改MySQL的配置或者设置c3p0的属性
2.可以通过如下语句查看wait_timeout的值:
①查看
show GLOBAL VARIABLES like '%timeout%'; #全局,和mysql中的my.cnf中的配置一致
show VARIABLES like '%timeout%'; #会话
修改(也可以修改my.cnf配置文件并重启达到相同效果)
set GLOBAL interactive_timeout=604800; #24小时
②里面还有一个interactive_timeout,具体区别可以参看《MySQL - wait_timeout与interactive_timeout详解》
3.配置c3p0
①修改的配置
<property name="breakAfterAcquireFailure">false</property>
<property name="testConnectionOnCheckout">false</property>
<property name="testConnectionOnCheckin">false</property>
<property name="idleConnectionTestPeriod">3600</property> <!--多长时间检查一次,单位秒-->
<property name="acquireRetryAttempts">10</property>
<property name="acquireRetryDelay">1000</property>
需要在每次使用的时候getConnection()从连接池中获取Connection,并在使用完成之后进行close归还到连接池中。
②关于c3p0的每个配置属性的详细信息可参看官网《c3p0官网》
关于c3p0的重连配置以及介绍可参看《关于c3p0的重连机制》
以上。
MySQL连接超时处理的更多相关文章
- MySQL连接问题【如何解决MySQL连接超时关闭】
--MySQL连接问题[如何解决MySQL连接超时关闭] ------------------------------------------------转载 最近做网站有一个站要用到WEB网页采集器 ...
- 转 MySQL连接超时
在负载较重的MySQL服务器上,有时你偶尔会看到一些连接超时的错误,诸如: Can’t connect to MySQL server on ‘mydb’(110).如果当时你有多个连接请求,你会发现 ...
- 如何解决MySQL连接超时关闭
最近做网站有一个站要用到WEB网页采集器功能,当一个PHP脚本在请求URL的时候,可能这个被请求的网页非常慢慢,超过了mysql的 wait-timeout时间,然后当网页内容被抓回来后,准备插入到M ...
- mysql连接超时问题
前几天使用个脚本不停的查看redis队列中的事件.如果有则把事件取出来,然后进行一些数据库操作. 后来发现,每天的第一次有事件时都会到导致,找不到数据. 后来定位到问题,是mysql在连接长时间无活动 ...
- mysql 连接超时的问题
项目中用mycat做的分表分库,异步通知系统会连接mycat去查数据库数据,有时会抛异常提示mysql server has gone away.最初以为是mycat的问题,在修改了mycat的配置, ...
- mysql连接超时的问题
使用Hibernate + MySQL数据库开发,链接超时问题: com.mysql.jdbc.CommunicationsException: The last packet successfull ...
- mysql连接超时的问题处理
1. 内网 ts 连接mysql 有时候会连接失败, 原因是 连接超时, 当时所有服务器一起启动,抢占资源,导致连接超过10s. 现在增加一次连接机会, 增加一些日志. 2. 并且对mysql 全局参 ...
- mysql连接超时
这几天在跟踪一个项目的时候,老是发现mysql连接报timeout的异常. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException ...
- 解决MySQL连接超时Communications link failure due to underlying exception
最近在用一个MySQL的Java连接池的过程中,连接一晚上不释放,第二天就会造成超时的错误,查了一下原因,原来是因为MySQL默认的空闲等待时间是8个小时,一旦空闲超过8个小时,就会抛出异常.异常文本 ...
- mysql 连接超时解决方案: 怎样修改默认超时时间
mysql数据库有一个wait_timeout的配置,默认值为28800(即8小时). 在默认配置不改变的情况下,如果连续8小时内都没有访问数据库的操作,再次访问mysql数据库的时候,mysql数据 ...
随机推荐
- 15-numpy笔记-莫烦pandas-3
代码 import pandas as pd import numpy as np dates = pd.date_range('20130101', periods=6) df=pd.DataFra ...
- Leetcode练习
1. 两数相加 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...
- 柯里化currying + 隐式调用 = 一个有名的add面试题
柯里化 =================================== 维基百科解释: 柯里化,英语:Currying(果然是满满的英译中的既视感),是把接受多个参数的函数变换成接受一个单一参 ...
- SpringBoot整合FastDFS实现图片的上传
文件的上传和预览在web开发领域是随处可见,存储的方式有很多,本文采用阿里巴巴余庆大神开发的FastDFS进行文件的存储,FastDFS是一个分布式文件存储系统,可以看我上一篇博文,有安装和配置教程 ...
- biopython处理中蜂基因组
1.安装包 pip install bcbio-gff pprint 2.显示中蜂的序列 from Bio import SeqIO genome_name = 'GCF_001442555.1_AC ...
- appium--解决每次安装appium setting和Unlock
前戏 每次启动appium进行自动化的时候,都会提示我们需要安装appium setting和Unlock,而且还都要手动确认 那这两个文件是做什么的呢? Appium settings:用于设置网络 ...
- Linux性能优化实战学习笔记:第五十二讲
一.上节回顾 上一节,我们一起学习了怎么使用动态追踪来观察应用程序和内核的行为.先简单来回顾一下.所谓动态追踪,就是在系统或者应用程序还在正常运行的时候,通过内核中提供的探针,来动态追踪它们的行为,从 ...
- [LeetCode] 888. Fair Candy Swap 公平糖果交换
Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [HDU4867]Xor (线段树分治+类数位dp)
[HDU4867]Xor (线段树分治+类数位dp) 提供一种\((m+n) log a log m\)带有常数约\(\frac{1}{log n}\)的算法 处理询问,将后来加入的数算进序列中,则每 ...