1.1 错误信息:

Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 20,820,001 milliseconds ago.  The last packet sent successfully to the server was 20,820,002 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.
at sun.reflect.GeneratedConstructorAccessor29.newInstance(Unknown Source) ~[na:na]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_51]
at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_51]
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) ~[mysql-connector-java-5.1.29.jar:na]
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1129) ~[mysql-connector-java-5.1.29.jar:na]
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3988) ~[mysql-connector-java-5.1.29.jar:na]
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2598) ~[mysql-connector-java-5.1.29.jar:na]
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778) ~[mysql-connector-java-5.1.29.jar:na]
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2828) ~[mysql-connector-java-5.1.29.jar:na]
at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5372) ~[mysql-connector-java-5.1.29.jar:na]
at com.mchange.v2.c3p0.impl.NewProxyConnection.setAutoCommit(NewProxyConnection.java:881) ~[c3p0-0.9.1.1.jar:0.9.1.1]
at org.quartz.impl.jdbcjobstore.AttributeRestoringConnectionInvocationHandler.setAutoCommit(AttributeRestoringConnectionInvocationHandler.java:98) ~[quartz-2.2.1.jar:na]

1.2 解决方法

- 如果使用的是JDBC,在JDBC URL上添加?autoReconnect=true,如:

jdbc:mysql://10.10.10.10:3306/mydb?autoReconnect=true

- 如果是在Spring中使用DBCP连接池,在定义datasource增加属性validationQuerytestOnBorrow,如:

<bean id="vrsRankDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${countNew.jdbc.url}" />
<property name="username" value="${countNew.jdbc.user}" />
<property name="password" value="${countNew.jdbc.pwd}" />
<property name="validationQuery" value="SELECT 1" />
<property name="testOnBorrow" value="true"/>
</bean>

- 如果是在Spring中使用c3p0连接池,则在定义datasource的时候,添加属性testConnectionOnCheckintestConnectionOnCheckout,如:

<bean name="cacheCloudDB" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${cache.url}"/>
<property name="user" value="${cache.user}"/>
<property name="password" value="${cache.password}"/>
<property name="initialPoolSize" value="10"/>
<property name="maxPoolSize" value="${cache.maxPoolSize}"/>
<property name="testConnectionOnCheckin" value="false"/>
<property name="testConnectionOnCheckout" value="true"/>
<property name="preferredTestQuery" value="SELECT 1"/>
</bean>

参考

附录分析

When a c3p0-proxied Connection throws an SQLException, c3p0 examines
the Exception and the Connection to make a judgement about whether
the problem implies that the Connection should no longer be included
in the pool. c3p0 tests the Connection, and if the test fails, the
Connection will be excluded from the pool. What c3p0 is telling you here is that a Connection that previously
signalled an error and then failed a Connection test is still in use,
and has signalled another error. From c3p0's perspective, this is a
non-issue, it just means c3p0 doesn't have to do any kind of checks
or notifications, the Connection is already gone as far as the pool
is concerned. But c3p0 wonders why you'd still be using such a
Connection, and warns you about it. Usually, if a client continues to use a Connection that c3p0 has
correctly identified as broken, all further uses will provoke such an
exception, and the fix is to close the Connection and start over when
an application's Connection turns out to be dead. But, by the error
you're getting, it looks like your Connection is still live and okay
-- it's clearly communicating with the database. So, the issue is,
why did c3p0 deem the Connection dead if it is not? If you turn on DEBUG level logging (relevant loggers would be
com.mchange.v2.c3p0.impl.NewPooledConnection,
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool, and
com.mchange.v2.c3p0.impl.DefaultConnectionTester, unless you've
defined your own ConnectionTester), you can trace the testing and
invalidation of Connections, and try to understand why Connections
that seem okay are testing as broken. That will give you better
information about what's going on. That said, the only cost of this behavior is disconcerting warning
messages and somewhat faster churn of Connections through the pool.
c3p0 is erring on the side of caution -- it has reason to believe a
Connection is bad, so it's been excluded from the pool. It'd be nice
to know why apparently good Connections are failing Connection tests,
but if it is an infrequent occurrence, it's very little to worry
about. (If it's happening a lot, you should track it down.) 原文地址:http://www.cnblogs.com/davidwang456/p/4425913.html

mysql 连接丢失错误解决(转载)的更多相关文章

  1. Linux 下一个 Mysql error 2002 错误解决

    Linux 下一个 Mysql error 2002 错误解决     首先查看 /etc/rc.d/init.d/mysqld status 查看mysql它已开始.     假设启动的的话,先将数 ...

  2. pycharm中连接MySql出现 Exception in thread "main" java.lang.ClassNotFoundException: com.mysql.jdbc.的错误解决

    具体异常如下: 这个异常特别烦人,是mysql连接驱动的问题,可以用二步解决: 第一步 从网上下载驱动,从mysql-connector-java_8.0.16-1ubuntu16.04_all.de ...

  3. mysql远程连接以及错误解决&命令行基本操作

    现在大家的程序服务基本都是部署在云服务器上,今天我分享记录一下:使用mysql数据库过程中比较常见操作和遇到的问题 环境:lunix 系统(阿里云服务器,华为云服务器,腾讯云等均适用) + mysql ...

  4. IntelliJ IDEA web项目进行数据库连接时出现java.lang.ClassNotFoundException: com.mysql.jdbc.Driver错误解决办法

    首先看报错信息: 意思是找不到类:  com.mysql.jdbc.Driver.也就是说tomcat找不到MySQL数据库连接要用的jar包! 出现这种错误的原因是: 项目中没有导入这个jar包, ...

  5. mysql安装及错误解决

    #下载mysql源安装包shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm# 安装my ...

  6. Zookeeper 报ConnectionLostException连接丢失错误汇总

    我google出来的几个结果都没有用,这里写出来或许可以帮助别人,这些都是google出来的结果,没有经过验证是否真的能够解决问题. 当前zookeeper的连接状态是connecting,这个时候连 ...

  7. mysql服务器无法连接10055错误解决示例

        windows服务器下,mysql运行一段时间之后忽然无法连接,但是mysql服务启动正常,连接时报错: can't connect to mysql server on 'localhost ...

  8. 使用sqlyog或者navicat连接mysql提示1862错误解决

    mysql的bin目录下执行  mysqladmin -uroot -p password 依次输入旧密码.新密码.确认新密码 修改后重新使用sqlyog或navicat连接成功 问题解决!

  9. Digao 连接Mysql 连接不上解决办法

    错误一:No module named 'MySQLdb' 原因:python3连接MySQL不能再使用mysqldb,取而代之的是pymysql. 解决方法:在python的MySQL包中,即路径: ...

随机推荐

  1. MakeFile 文件详解

    GNU的make工作时的执行步骤入下:(想来其它的make也是类似)      1.读入所有的Makefile.      2.读入被include的其它Makefile.      3.初始化文件中 ...

  2. sendkeys用法详解

    将一个或多个按键消息发送到活动窗口,就如同在键盘上进行输入一样. 语法 SendKeys string[, wait] SendKeys 语句的语法具有以下几个命名参数: 部分 描述   string ...

  3. 【改进】用Log4net建立日志记录

    上一篇随笔中只使用了普通的文件读写来进行日志的写入,正如很多朋友说的,频繁的对文件进行读写会造成很多的问题,代码缺少边界控制和操作控制,没有对资源进行管理,是非常典型的bad code. 然后经过前辈 ...

  4. 学习ajax 总结

    一.服务器客户端基础知识 通信是指不同计算机程序的通信,单单通过ip地址就能知道你找的是哪一台计算机,但是不知道是计算机上的哪个应用程序,要想知道是哪个程序就必须通过端口.这时候就可以问端口到底是什么 ...

  5. InternetOpen怎么使用代理

    如果你用IE的默认代理设置:hinternet=InternetOpen(AfxGetAppName(),INTERNET_OPEN_TYPE_PROXY,NULL,NULL,0); 把INTERNE ...

  6. UIViewSubviews多个views之间的关系

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  7. C#--格式化数值数据

    如果数值数据需要更精细的格式化,每一个占位符都可以包含不同的格式字符,下表展示了核心格式化选项.   下面用一个例子来说明. namespace LearningCSharp { class Prog ...

  8. C#消息模拟

    C#中消息的工作流程: C#中的消息被Application类从应用程序消息队列中取出,然后分发到消息对应的窗体,窗体对象的第一个响应函数是对象中的protected override void Wn ...

  9. ASP.NET MVC 路由进阶(之二)--自定义路由约束

    3.自定义路由约束 什么叫自定义路由约束呢?假如路由格式为archive/{year}/{month}/{day},其中year,month,day是有约束条件的,必须是数字,而且有一定范围. 这时候 ...

  10. javascript 笔记——bind 用法小技巧

    $(function(){ //代码阅读能力 function speak(name){ return "Hello " + name; } //此处写代码 //方法一 //重点是 ...