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. [改善Java代码]使用构造块精炼程序

    建议36: 使用构造代码块精炼程序 什么叫代码块(Code Block)?用大括号把多行代码封装在一起,形成一个独立的数据体,实现特定算法的代码集合即为代码块,一般来说代码块是不能单独运行的,必须要有 ...

  2. [改善Java代码]不要只替换一个类

    建议20: 不要只替换一个类 我们经常在系统中定义一个常量接口(或常量类),以囊括系统中所涉及的常量,从而简化代码,方便开发,在很多的开源项目中已采用了类似的方法,比如在Struts2中,org.ap ...

  3. poj 2411 状态压缩dp

    思路:将每一行看做一个二进制位,那么所有的合法状态为相邻为1的个数一定要为偶数个.这样就可以先把所有的合法状态找到.由于没一层的合法状态都是一样的,那么可以用一个数组保存.由第i-1行到第i行的状态转 ...

  4. CSS3如何实现2D转换和3D转换,他们有何区别

    CSS3中2D3D技术的发展,带来了更丰富的视觉效果~他们的实现机理是怎样的呢? 1定义 2D: 能够对元素进行移动,缩放,转动,拉长或拉伸. 3D: 允许对元素进行格式化,在三维空间进行操作.元素改 ...

  5. ActiveMQ(5.10.0) - Configuring the Simple Authentication Plug-in

    The easiest way to secure the broker is through the use of authentication credentials placed directl ...

  6. 【网络收集】MySql中IS NOT NULL与!=NULL的区别

    在mysql中,筛选非空的时候经常会用到is not null和!=null,这两种方法单从字面上来看感觉是差不多的,其实如果去运行一下试试的话差别会很大!为什么会出现这种情况呢?null 表示什么也 ...

  7. cv::mat转换成QImage的问题

    在进行cv::mat转换为QImage过程中,经常出现问题: cv::Mat image; ...QImage img=QImage((const unsigned char*)(image.data ...

  8. 关于在windows7中使用Virtual Box 按照 安卓虚拟机几个注意事项

    1.选择安卓原生镜像的问题 选择带PC的字眼的,也就是给平板PC使用的那个,我使用的版本是android-x86-4.0-r1-eeepc.iso其他类似版本也是可以的,因为我已经成功实践啦. 下载地 ...

  9. [zz]安装PostgreSQL数据库(Linux篇)

    0.编译环境 Linux: CentOS 5.5 gcc: 4.1.2 1. 安装PostgreSQL 1) 解压postgresql-9.1.7.tar.bz2 #tar jxvf postgres ...

  10. 滑动选择日期(基于sui-mobile的移动端)

    $(page).on('touchmove','#touchMoveTime',function (event) { touchMove(event); }); scrollBarInit(); // ...