`

关于MySQL的wait_timeout连接超时问题报错解决方案

 
阅读更多
Mysql服务器默认的“wait_timeout”是8小时【也就是默认的值默认是28800秒】,也就是说一个connection空闲超过8个小时,Mysql将自动断开该connection,通俗的讲就是一个连接在8小时内没有活动,就会自动断开该连接。 wait timeout的值可以设定,但最多只能是2147483,不能再大了。也就是约24.85天  所以即使你MySQL通过my.ini 在  # The TCP/IP Port the MySQL Server will listen on port=3306下面添加  # this is myown dinifition for mysql connection timeout wait_timeout=31536000  interactive_timeout=31536000  无论超过最大限度多大的数值,只能被MySQL解析为2147483,2147483天后你的程序该出什么错还是什么错,避免不了的

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增加属性validationQuery和testOnBorrow,如:

<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的时候,添加属性testConnectionOnCheckin和testConnectionOnCheckout,如:

<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>
参考

MySQL reconnect issues


附录分析

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://sourceforge.net/p/c3p0/mailman/message/18310863/


mysql重连,连接丢失:The last packet successfully received from the server--转载

http://www.cnblogs.com/davidwang456/p/4425913.html
分享到:
评论

相关推荐

    关于MySQL的wait-timeout连接超时问题报错解决方案.pdf

    解决连接超时问题需要从多方面考虑,包括调整 wait_timeout 参数的值、使用 Connector/J 连接属性、及时释放连接等方法。只有通过合适的方法,才能避免连接超时问题的出现,提高系统性能和稳定性。

    关于MySQL的wait-timeout连接超时问题报错解决方案.docx

    MySQL 连接超时问题是常见的错误之一,解决该问题需要了解 wait_timeout 参数的重要性,并正确地使用 show variables 命令。同时,在使用 Hibernate 框架进行数据库操作时,需要注意连接超时问题,并采取相应的解决...

    MySQL wait_timeout连接超时

    MySQL的`wait_timeout`连接超时问题通常出现在长时间无操作的数据库连接上,服务器会自动关闭这些连接以释放资源。这种现象在应用中可能导致突然的数据通信中断,表现为“Communications link failure”等错误,提示...

    MySQL数据库连接超时(Wait_timeout)问题总结.pdf

    总之,MySQL的连接超时问题主要是由`wait_timeout`参数引发的,解决方法包括但不限于调整MySQL服务器配置、使用连接池、更新应用程序代码以处理连接失效的情况,以及在使用ORM框架时,按照框架的文档配置相应的连接...

    MySQL中interactive_timeout和wait_timeout的区别

    在MySQL中,`interactive_timeout` 和 `wait_timeout` 是两个重要的配置参数,它们与客户端连接到服务器的超时设置紧密相关。理解这两个参数的区别对于优化数据库性能和避免不必要的连接断开至关重要。 `...

    mysql提示got timeout reading communication packets的解决方法

    错误提示: user: ‘root’ host: `localhost’ (Got timeout reading communication ...wait_timeout 的默认值这:120 根据情况增加吧. 这两个值是一个全局变量,可以动态增加,如: mysql&gt; set global interactiv

    mysql 8小时空闲后连接超时的问题

    需要注意的是,尽管调整`interactive_timeout`可以解决连接超时问题,但过度增加超时时间可能会导致服务器资源被长时间占用,尤其是在高并发的环境中。因此,优化数据库连接管理和使用连接池是更推荐的做法。 连接...

    javaee mysql空闲关闭

    然而,为了优化资源使用,MySQL配置了一个参数`wait_timeout`,这个参数定义了连接在空闲多长时间后会被自动关闭。当JavaEE应用中的数据库连接在超过这个设定的时间没有执行任何操作,MySQL服务器会断开这个连接,这...

    还原大备份mysql文件失败的解决方法分享

    2. **wait_timeout** 和 **interactive_timeout**: 这两个参数分别定义了非交互式连接和交互式连接在无活动状态后等待多久关闭。在上述例子中,它们都被设置为2880000秒(约80小时)。这有助于防止长时间运行的还原...

    关于mysql提示too many connections问题解决方法.doc

    - `SET GLOBAL wait_timeout=120`:设置非交互式连接的超时时间,单位为秒。如果超过这个时间没有活动,连接将被自动断开。 - `SET GLOBAL interactive_timeout=300`:设置交互式连接的超时时间。与`wait_timeout`...

    解决SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded_runtimeerror怎么修复

    在MySQL数据库操作中,"SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded" 是一个常见的错误,它意味着在执行事务时,系统等待锁定资源的时间超过了预设的限制。这个错误通常发生在并发环境中,当...

    MySQL和连接相关的timeout 的详细整理

    `wait_timeout`和`interactive_timeout`则是关于连接空闲时的超时设置。`wait_timeout`用于非交互式连接,即不进行任何查询操作的连接,如果在指定时间内没有活动,服务器将自动关闭连接。而`interactive_timeout`是...

    解决MySQL 5数据库连接超时问题

    mysql5将其连接的等待时间(wait_timeout)缺省为8小时。怎么不让它超时呢

    SQL查询超时的设置方法(关于timeout的处理)

    通过对`connect_timeout`、`interactive_timeout`、`wait_timeout`等参数的调整,可以更好地控制连接的生命周期,防止因长时间无响应导致的问题,并确保系统能够有效地处理各种查询。在进行调整时,务必根据实际情况...

    Spring Boot如何解决Mysql断连问题

    Spring Boot 解决 Mysql ...解决 Spring Boot 连接 Mysql 断连问题可以使用多种方法,包括在 application.properties 文件中添加配置项、修改 Mysql 的 wait_timeout 参数等。选择合适的方法可以根据实际情况进行选择。

Global site tag (gtag.js) - Google Analytics