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连接超时处理的更多相关文章

  1. MySQL连接问题【如何解决MySQL连接超时关闭】

    --MySQL连接问题[如何解决MySQL连接超时关闭] ------------------------------------------------转载 最近做网站有一个站要用到WEB网页采集器 ...

  2. 转 MySQL连接超时

    在负载较重的MySQL服务器上,有时你偶尔会看到一些连接超时的错误,诸如: Can’t connect to MySQL server on ‘mydb’(110).如果当时你有多个连接请求,你会发现 ...

  3. 如何解决MySQL连接超时关闭

    最近做网站有一个站要用到WEB网页采集器功能,当一个PHP脚本在请求URL的时候,可能这个被请求的网页非常慢慢,超过了mysql的 wait-timeout时间,然后当网页内容被抓回来后,准备插入到M ...

  4. mysql连接超时问题

    前几天使用个脚本不停的查看redis队列中的事件.如果有则把事件取出来,然后进行一些数据库操作. 后来发现,每天的第一次有事件时都会到导致,找不到数据. 后来定位到问题,是mysql在连接长时间无活动 ...

  5. mysql 连接超时的问题

    项目中用mycat做的分表分库,异步通知系统会连接mycat去查数据库数据,有时会抛异常提示mysql server has gone away.最初以为是mycat的问题,在修改了mycat的配置, ...

  6. mysql连接超时的问题

    使用Hibernate + MySQL数据库开发,链接超时问题: com.mysql.jdbc.CommunicationsException: The last packet successfull ...

  7. mysql连接超时的问题处理

    1. 内网 ts 连接mysql 有时候会连接失败, 原因是 连接超时, 当时所有服务器一起启动,抢占资源,导致连接超过10s. 现在增加一次连接机会, 增加一些日志. 2. 并且对mysql 全局参 ...

  8. mysql连接超时

    这几天在跟踪一个项目的时候,老是发现mysql连接报timeout的异常. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException ...

  9. 解决MySQL连接超时Communications link failure due to underlying exception

    最近在用一个MySQL的Java连接池的过程中,连接一晚上不释放,第二天就会造成超时的错误,查了一下原因,原来是因为MySQL默认的空闲等待时间是8个小时,一旦空闲超过8个小时,就会抛出异常.异常文本 ...

  10. mysql 连接超时解决方案: 怎样修改默认超时时间

    mysql数据库有一个wait_timeout的配置,默认值为28800(即8小时). 在默认配置不改变的情况下,如果连续8小时内都没有访问数据库的操作,再次访问mysql数据库的时候,mysql数据 ...

随机推荐

  1. 图学Kubernetes

    所有图片来自:Kubernetes Patterns: Reusable Elements for Designing Cloud-Native Applications 本文图片摘要曾经在某大厂内网 ...

  2. LeetCode 51. N-QueensN皇后 (C++)(八皇后问题)

    题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...

  3. LeetCode 79. Word Search单词搜索 (C++)

    题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fr ...

  4. oracle左连接与右连接

    left join(左联接)       ---返回左表中的所有记录和右表中条件字段相等的记录. right join(右联接)     ---返回右表中的所有记录和左表中联结字段相等的记录 inne ...

  5. [LeetCode] 920. Number of Music Playlists 音乐播放列表的个数

    Your music player contains N different songs and she wants to listen to L (not necessarily different ...

  6. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  7. Python 爬取 13 个旅游城市,告诉你五一大家最爱去哪玩?

    五一假期已经结束,小伙伴是不是都还没有玩过瘾?但是没办法,还有很多bug等着我们去写,同样还有需要money需要我们去赚.为了生活总的拼搏. 今年五一放了四天假,很多人不再只是选择周边游,因为时间充裕 ...

  8. Ethics

    1.What are Ethics? Ethics describe a code of behaviour 2.Why Ethics in Data Science is important? da ...

  9. 转载:string、const char*、 char* 、char[]相互转换

    本文转自:https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 一:转化总结形式如下: 使用时,要对源格式和目标格式进行初始化 ...

  10. java跳出循环break;return;continue使用

    for(int i=0;i<5;i++){ if(i==2){ System.out.println("i==2时忽略了"); continue;//忽略i==2时的循环 } ...