`
wuzhengxuan
  • 浏览: 21795 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Mysql5的auto Reconnect错误

阅读更多

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

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

Java代码  收藏代码
  1.     
  2.   
  3. com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:   
  4.   
  5.    
  6.   
  7. ** BEGIN NESTED EXCEPTION **   
  8.   
  9.    
  10.   
  11. java.io.EOFException  
  12.   
  13. MESSAGE: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.  
  14.   
  15.    
  16.   
  17. STACKTRACE:  
  18.   
  19.    
  20.   
  21. java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.  
  22.   
  23.         at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1997)  
  24.   
  25.         at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2411)  
  26.   
  27.         at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2916)  
  28.   
  29.         at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)  
  30.   
  31.         at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)  
  32.   
  33.         at com.mysql.jdbc.Connection.execSQL(Connection.java:3250)  
  34.   
  35.         at com.mysql.jdbc.Connection.setAutoCommit(Connection.java:5395)  
  36.   
  37. ...  





我们首先判断是连接池出了问题,即当系统时间改变后,数据库连接发现超时,因此需要重新连接或完全重建连接池。我们换用了一下数据库,如果使用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,问题就这样解决了! 


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

以上内容大部分是我同事执笔写的,我做了微量的修改,主要是和大家分享一下经验。 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics