`

Mysql5的auto Reconnect错误

阅读更多

一、解决方案一

最近在一个J2EE项目的开发过程中,遇到了这样的问题:

在服务器上部署好这个Web系统后,这时访问系统是很正常的。当把服务器的时间(例如:2008-03-31)加一天或更多天(例如:2008-04-01,2008-04-02...),这时再访问这个Web系统,报出如下的异常:

QUOTE:
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.io.EOFException

MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

STACKTRACE:

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:1997)

at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2411)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2916)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)

at com.mysql.jdbc.Connection.execSQL(Connection.java:3250)

at com.mysql.jdbc.Connection.setAuto Commit(Connection.java:5395)

...


我们首先判断是连接池出了问题,即当系统时间改变后,数据库连接发现超时,因此需要重新连接或完全重建连接池。我们换用了一下数据库,如果 使用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 属性,可以设置一段时间后连接池自动测试执行,保持连接开放,效果也是一样的。

二、解决方案二

MySQL官方不推荐使用autoReconnect=true,参见http://bugs.mysql.com/bug.php?id=5020
需要另外找别的办法来解决超过8小时,链接断开的问题。

由于问题产生的根本原因在于服务到数据库的连接长时间没活动,既然重新连接的办法无效,就可以尝试另外一种办法,就是反空闲。
自己写一个线程来反空闲的话,比较麻烦。
最后在网上找到一个办法。为hibernate配置连接池,推荐用c3p0,然后配置c3p0的反空闲设置idle_test_period,只要小于MySQL的wait timeout即可。
在hibernate.cfg.xml中增加下面几项:

<!-- configuration pool via c3p0-->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">30</property>
<property name="c3p0.time_out">1800</property> <!-- seconds --><!-- default: 0 -->
<property name="c3p0.max_statement">50</property> <!-- default: 0 -->
<property name="c3p0.acquire_increment">1</property> <!-- default: 1 -->
<property name="c3p0.idle_test_period">120</property>  <!-- seconds --><!-- default: 0 -->
<property name="c3p0.validate">true</property>
三、解决方案三
解决办法: FuIU1|<&  
!@]{k?j  
如果连接闲置8小时 (8小时内没有进行数据库操作), mysql就会自动断开连接, 要重启tomcat. 7,yVM  
    不用hibernate的话, connection url加参数: autoReconnect=true YHy&; -5  
    用hibernate的话, 加如下属性: a_Zbt  
        <property name="connection.autoReconnect">true</property> vm sFu(i  
        <property name="connection.autoReconnectForPools">true</property> P @:f+  
        <property name="connection.is-connection-validation-required">true</property>
uY}.T�k  
    要是还用c3p0连接池: H <|^f!  
        <property name="hibernate.c3p0.acquire_increment">1</property> 'zcyrR"  
        <property name="hibernate.c3p0.idle_test_period">0</property> \F f%m  
        <property name="hibernate.c3p0.timeout">0</property> ug(QB  
        <property name="hibernate.c3p0.validate">true</property>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics