`
fencering
  • 浏览: 22775 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Mysql5的auto Reconnect错误

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

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

  

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.setAutoCommit(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数据库连接池是比较合适的。

以上内容大部分是我同事执笔写的,我做了微量的修改,主要是和大家分享一下经验。
分享到:
评论
4 楼 hhsc00 2012-05-15  
连接字符串URL中,将localhost改成127.0.0.1,就OK了。

原因见:
Testing your connectivity with the mysql command-line client will not work unless you add the –host flag, and use something other than localhost for the host. The mysql command-line client will use Unix domain sockets if you use the special host name localhost. If you are testing connectivity to localhost, use 127.0.0.1 as the host name instead.

引用自:http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-troubleshooting.html
3 楼 hama 2008-04-22  
那你也可以用dbcp的
validationQuery:SELECT 1

_testOnBorrow _testOnReturn _testWhileIdle:最好都设为true

_minEvictableIdleTimeMillis:大于0 ,进行连接空闲时间判断,或为0,对空闲的连接不进行验证

_timeBetweenEvictionRunsMillis:失效检查线程运行时间间隔,如果小于等于0,不会启动检查线程
2 楼 xly_971223 2008-04-07  
dbcp对自动连接处理的不够好 可以试试c3p0
1 楼 myreligion 2008-04-07  
每次都test估计会降低性能。

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

相关推荐

Global site tag (gtag.js) - Google Analytics