`

Mysql5的auto Reconnect错误

    博客分类:
  • J2EE
阅读更多
在服务器上部署好这个Web系统后,这时访问系统是很正常的。当把服务器的时间(例如:2008-03-31)加一天或更多天(例如:2008-04-01,2008-04-02...),这时再访问这个Web系统,报出如下的异常:
org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'saveMessage' on target class [class com.sun.proxy.$Proxy8] failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: 
	at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:273)
	at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
Caused by: org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: 
	at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
	at com.sun.proxy.$Proxy8.saveMessage(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor380.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
	at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264)
	... 3 more
Caused by: org.hibernate.TransactionException: JDBC begin failed: 
	at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:92)
	at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
	at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
	... 14 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 31,293,844 milliseconds ago.  The last packet sent successfully to the server was 1 milliseconds ago.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3589)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3478)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4019)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728)
	at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5075)
	at com.mysql.jdbc.jdbc2.optional.ConnectionWrapper.setAutoCommit(ConnectionWrapper.java:154)
	at sun.reflect.GeneratedMethodAccessor376.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.atomikos.jdbc.AtomikosConnectionProxy.invoke(AtomikosConnectionProxy.java:157)
	at com.sun.proxy.$Proxy5.setAutoCommit(Unknown Source)
	at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:87)
	... 16 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
	at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3039)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3489)
	... 29 more
2013-11-14 08:34:35,175-[QC] ERROR [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-7] org.quartz.core.ErrorLogger.schedulerError(2015) | Job (DEFAULT.messageServiceDetail threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'saveMessage' on target class [class com.sun.proxy.$Proxy8] failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: ]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:214)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
* Nested Exception (Underlying Cause) ---------------
org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'saveMessage' on target class [class com.sun.proxy.$Proxy8] failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: 
	at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:273)
	at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
	at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)
Caused by: org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC begin failed: 
	at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:596)
	at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:335)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
	at com.sun.proxy.$Proxy8.saveMessage(Unknown Source)
	at sun.reflect.GeneratedMethodAccessor380.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
	at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:264)
	... 3 more
Caused by: org.hibernate.TransactionException: JDBC begin failed: 
	at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:92)
	at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
	at org.springframework.orm.hibernate3.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:555)
	... 14 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 31,293,844 milliseconds ago.  The last packet sent successfully to the server was 1 milliseconds ago.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
	at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3589)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3478)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4019)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2728)
	at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5075)
	at com.mysql.jdbc.jdbc2.optional.ConnectionWrapper.setAutoCommit(ConnectionWrapper.java:154)
	at sun.reflect.GeneratedMethodAccessor376.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at com.atomikos.jdbc.AtomikosConnectionProxy.invoke(AtomikosConnectionProxy.java:157)
	at com.sun.proxy.$Proxy5.setAutoCommit(Unknown Source)
	at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:87)
	... 16 more
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
	at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3039)
	at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3489)
	... 29 more

我们首先判断是连接池出了问题,即当系统时间改变后,数据库连接发现超时,因此需要重新连接或完全重建连接池。我们换用了一下数据库,如果使用Oracle数据库,同样的修改不会出现问题,系统运行正常,根据不会出现这样的异常。这就说明了出现这个异常是Mysql相关的,通过查阅资料得知可能和Mysql的运行参数(超时)有关,顺着这个思路我们来分析Mysql的一些运行参数。

在MySQL Command Line Client中执行show variables like '%timeout%';如下图所示:

在图中我们可以看到有两个变量wait_timeout和interactive-timeout,它们的默认值都为28800秒,即为8小时。也就是说默认情况下,Mysql在经过8小时(28800秒)不使用后会自动关闭已打开的连接。

为了解决这个问题,对于MySQL5之前的版本,如Mysql4.x,只需要修改连接池配置中的URL,添加一个参数:autoReconnect=true,如果是MySQL5及以后的版本,则需要修改my.cnf(或者my.ini)文件,在[mysqld]后面添加上:

wait_timeout = n

interactive-timeout = n

n为服务器关闭交互式连接前等待活动的秒数。

可是就部署而言每次修改my.ini比较麻烦,而且n等于多少才是合适的呢?所以这个解决办法不好。

怎么才能比较好的解决这个问题呢?通过仔细分析,原因大致为:当修改系统日期后Mysql自动关闭已打开的连接,可以数据库连接池(DBCP)还认为这些连接是活动的,如果这时有请求(需要执行读写数据库的操作),连接池就用一个连接去操作数据库,而这个连接在Mysql的连接池中并不存在,所以会出现以上的异常。如果一个连接在和Mysql建立连接时能检查就不会有这样的问题了。

这时我们同事想起了他以前用过的一个数据库连接池proxool,它有两个属性:一个是test-before-use,还有一个是test-after-use,这两个属性就是在使用前和使用后都要进行对连接的检查,如果连接无效就扔掉再创建一个新的连接,它们的解释如下:

test-before-use:

If you set this to true then each connection is tested (with whatever is defined in house-keeping-test-sql) before being served. If a connection fails then it is discarded and another one is picked. If all connections fail a new one is built. If that one fails then you get an SQLException saying so.

test-after-use:

If you set this to true then each connection is tested (with whatever is defined in house-keeping-test-sql) after it is closed (that is, returned to the connection pool). If a connection fails then it is discarded.

于是我们在项目中换上proxool的连接池,再运行并访问系统,更改系统时间,再访问,一切OK,问题就这样解决了!

不但如此,proxool的连接池中的连接的数量,活动数以及每个连接的的具体情况都可以一目了然的看到,如下图所示:

通过上面的测试和分析,针对于Mysql5数据库,选择proxool数据库连接池是比较合适的。

c3p0的连接池有 idleConnectionTestPeriod 属性,可以设置一段时间后连接池自动测试执行,保持连接开放,效果也是一样的。
分享到:
评论

相关推荐

    Colab Auto Reconnect-crx插件

    **谷歌Colab Auto Reconnect-crx插件详解** 在日常使用谷歌Colaboratory(简称Colab)进行Python编程和数据分析时,我们可能会遇到网络不稳定导致的连接中断问题。这不仅打断了我们的工作流程,也可能丢失运行中的...

    irccloud-auto-reconnect:自动重新连接IRCCloud

    标题 "irccloud-auto-reconnect: 自动重新连接IRCCloud" 涉及的是一个针对IRCCloud服务的自动化解决方案,旨在解决用户在使用IRCCloud时遇到的网络中断问题。IRCCloud是一款云基础的IRC(Internet Relay Chat)...

    mysql 5.5版 头文件(mysql.h等)

    虽然不是直接由头文件提供,但可以使用`mysql_options()`设置`MYSQL_OPT_RECONNECT`和`MYSQL_OPT_CONNECT_TIMEOUT`等相关选项来管理连接池。 以上就是MySQL 5.5版头文件中的核心知识点。了解并熟练运用这些函数和...

    IDEA链接MySQL报错08001和连接成功后不显示表的问题及解决方法

    报错Connection to blog0@localhost failed....到此这篇关于IDEA链接MySQL报错08001和连接成功后不显示表的问题及解决方法的文章就介绍到这了,更多相关IDEA链接MySQL不显示表内容请搜索软件开发网以前的文章或继续

    MySQL接口的代码

    在使用MySQL接口时,应关注错误处理,如检查返回值、捕获异常。同时,了解如何优化SQL语句、使用索引、批量操作等方法,能够提高MySQL接口的性能。 7. **连接池与持久化连接**: 对于高并发应用,可以考虑使用...

    The_MySQL_C_API_编程实例

    5. `mysql_free_result`:释放内存,用于不再需要的`MYSQL_RES`对象。 6. `mysql_errno`和`mysql_error`:检查和获取错误信息。 7. `mysql_close`:关闭数据库连接。 在RedHat环境下,你需要确保已经安装了`...

    CodeBlocks连接Mysql数据库解决文档

    3. 在代码中使用`mysql_options()`函数设置`MYSQL_OPT_RECONNECT`选项,以便在连接断开后自动重连,因为`caching-sha2-password`可能会在认证过程中断开连接。 为了成功完成上述步骤,你需要下载和安装合适的MinGW...

    rails和mysql数据库连接中出现的问题以及解决办法

    如果这些信息填写错误或者遗漏,Rails将无法建立到MySQL的连接。确保以下内容正确无误: ```yaml development: adapter: mysql2 encoding: utf8mb4 reconnect: false database: your_database_name pool: 5 ...

    MYSQL的C的API实例讲解

    5. 最后,使用`mysql_free_result`释放结果集的内存。 此外,还有一些其他重要的API函数,如`mysql_query`用于执行SQL查询,`mysql_error`和`mysql_errno`用于检查和报告错误,`mysql_close`用于关闭数据库连接。 ...

    Reconnect3.java

    Reconnect3.java

    MySQL相关接口

    if (mysql_options(conn, MYSQL_SET_CLIENT_OPTION, &MYSQL_OPT_RECONNECT) || mysql_options(conn, MYSQL_SET_CLIENT_KEY, ssl_key) || mysql_options(conn, MYSQL_SET_CLIENT_CERT, ssl_cert) || mysql_options...

    mysql 连接闪断自动重连的实例

    总之,处理MySQL连接闪断并实现自动重连是一个关键的错误恢复策略,它确保了应用程序在面对网络问题时仍能保持服务连续性。通过合理的设计和编程实践,我们可以构建出健壮的数据库连接管理机制。

    使用Perl DBI操作MySQL的一些建议

    从DBD::mysql 4.012版本开始,可以通过设置`mysql_auto_reconnect`属性为1来实现自动重连: ```perl $dbh->{mysql_auto_reconnect} = 1; ``` 对于不支持此特性的早期版本,可以设置`wait_timeout`和`interactive...

    mysql数据库基础教程.pdf

    - `reconnect`或`r`:重新连接服务器。 - `exit`或`quit`:退出MySQL。 - `go`或`g`:执行命令。 - `print`或`p`:打印当前命令。 - `status`或`s`:显示服务器状态。 - `use dbname`:切换到指定数据库dbname。 ...

    libevent使用案例

    此案例主要实现了客户端与服务端的基本会话的功能,客户端发送消息给服务端,服务端回传给单个或所有客户端。 修改服务端中的全局变量 g_bSendAll 可控制服务端...客户端中的宏定义 AUTO_RECONNECT可控制是否自动重连

    node-reconnect-ws:节点中Websocket的重新连接包装器

    yarn add node-reconnect-ws用法const nodeReconnectWs = require ( 'node-reconnect-ws' )const wsc = new nodeReconnectWs ( { url : url , protocol : [ ] , reconnectInterval : 4000 , autoConnect : true , ...

Global site tag (gtag.js) - Google Analytics