mysql 连接丢失错误解决(转载)

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
时间: 2024-08-25 16:05:30

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

win10系统下安装Navicat for MySql 连接出现错误解决方法

在安装Navicat for Mysql完成后,连接时出现"1045-Acess denied for user:'root'@'localhost'(Using password:YES)" 这样的信息. 上网查了以后明白出现这样的提示原因是ROOT密码错误.灵机一动就是有默认密码.如果默认密码没有去修改的话,这里需要注意一点的是这里填写的密码要填写默认密码:root 如图这样是错误的: 因为我以为图中密码要自己去设置. 记住一点:密码填写 root 这是自己今天安装时遇到问题的心得

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

首先看报错信息: 意思是找不到类:  com.mysql.jdbc.Driver.也就是说tomcat找不到MySQL数据库连接要用的jar包! 出现这种错误的原因是: 项目中没有导入这个jar包, 或者导入路径错误. 经过检查发现我的出错原因是: 直接在web目录下新建目录lib, 然后把mysql连接jar包拷贝到lib里面, 再右击选择Add as Library. 最后运行直接报错了. 对比之前在eclipse上写的项目, 这个lib目录是在 WebContent\WEB-INF 下的,

MySQL启动1053错误解决方法

创建mysql服务时,系统已提示创建服务成功,但是net start命令提示启动失败,并在services.msc中提示1053错误 操作系统:windows7 mysql版本:5.5.22 www.jb51.net 在使用命令mysqld --install servicename --defaults-file='file_name'创建mysql服务时,系统 已提示创建服务成功,但是net start命令提示启动失败,并在services.msc中提示1053错误 解决办法: 在网上有人说

mysql安装及错误解决

#下载mysql源安装包shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm# 安装mysql源shell> yum localinstall mysql57-community-release-el7-8.noarch.rpm检查mysql源是否安装成功shell> yum repolist enabled | grep "mysql.*-community.*"

Win7x64中使用PowerDesigner连接Oralce数据库报“[Oracle][ODBC][Ora]ORA-12154:TNS:无法解析指定的连接标识符”错误解决方法

错误描述 操作系统是Win7 x64,Oracle服务端是Oracle11g x64,客户端是Oracle10gR2 x86. 使用PL-SQL Developer连接正常 使用系统ODBC连接正常.这里要注意的是,64位系统默认启动的是64位ODBC数据源管理器,默认指向的是服务端连接,所以需要为Oracle服务端也配置TNS Service Name,否则同样会连接报错.PowerDesigner默认启动的是32位的ODBC数据源管理器,程序路径为“%SystemRoot%\SysWOW64

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

错误一:No module named 'MySQLdb' 原因:python3连接MySQL不能再使用mysqldb,取而代之的是pymysql. 解决方法:在python的MySQL包中,即路径:C:\Users\adong\AppData\Local\Programs\Python\Python36\Lib\site-packages\Django-2.0.3-py3.6.egg\django\db\backends\mysql 下的__init__.py文件中加入: import pym

mysql报1236错误解决方法

mysql slave状态如下,发现IO线程为NO状态,并且报1236错误代码 mysql> show slave status\G *************************** 1. row ***************************                Slave_IO_State:                    Master_Host: 10.0.0.20                   Master_User: rep             

mysql导出数据错误解决办法

mysqldum导出大数据表错误[[email protected] itop]# mysqldump -u root -p --opt itop >/home/itop/itop-backup.sqlEnter password: mysqldump: Error 2020: Got packet bigger than 'max_allowed_packet' bytes when dumping table attachment at row: 1 解决办法:增加mysqldump的 ma

Mysql Error 1142错误解决方法

Error:ALTER command denied to user 'xxxx'@'localhost' for table 'uc_notelist' Errno:1142 SQL::ALTER TABLE uc_notelist ADD COLUMN app1 tinyint NOT NULL 说明xxxx用户没有ALTER权限. 到mysql中, mysql> SELECT alter_priv FROM user WHERE  User = 'xxxx'; 发现alter_priv =