最近在一个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,问题就这样解决了!
通过上面的测试和分析,针对于Mysql5数据库,选择proxool数据库连接池是比较合适的。
以上内容大部分是我同事执笔写的,我做了微量的修改,主要是和大家分享一下经验。
分享到:
相关推荐
语言:English 防止意外断开连接的工具 一种无需单击按钮即可自动重新连接Google Colab的工具。
报错Connection to blog0@localhost failed....到此这篇关于IDEA链接MySQL报错08001和连接成功后不显示表的问题及解决方法的文章就介绍到这了,更多相关IDEA链接MySQL不显示表内容请搜索软件开发网以前的文章或继续
Reconnect3.java
此案例主要实现了客户端与服务端的基本会话的功能,客户端发送消息给服务端,服务端回传给单个或所有客户端。 修改服务端中的全局变量 g_bSendAll 可控制服务端...客户端中的宏定义 AUTO_RECONNECT可控制是否自动重连
mysql连接过多导致总是死掉,后来网上搜索发现此文自Mysql 5.x的某个版本之后,Mysql的自动关闭空闲连接的特性修改了,如果一个连接空闲到超时时间(默认28000秒8小时),再次发起的Reconnect重新连接请求不会被...
自动重新连接自动重新连接 。 该脚本适用于非订户。 当您打开新的IRCCloud页面时,此脚本将重新连接到所需的断开连接的网络。 支持的浏览器:带有Firefox和带有Chrome
yarn add node-reconnect-ws用法const nodeReconnectWs = require ( 'node-reconnect-ws' )const wsc = new nodeReconnectWs ( { url : url , protocol : [ ] , reconnectInterval : 4000 , autoConnect : true , ...
MySQL和连接相关的timeout ...The number of seconds to wait for more data from the master before the slave considers the connection broken, aborts the read, and tries to reconnect. The fi
ActiveRecord::Mysql::UuidColumn 向ActiveRecord MySQL2适配器添加一个:uuid列,该列存储... mysql2 username: root host: localhost reconnect: true database: awesome_project_of_yours添加列类型uuid ActiveReco
用法 var reconnect = require ( 'net-socket-reconnect' )var client = reconnect ( { port : 8080 , reconnectInterval : 150 } ) // no change to original API选项reconnectOnError在“错误”时reconnectOnError...
SQL权限 SQLight是一个用C ++ 11编写的轻量级简单MySQL客户端。 SQLight返回JSON文档。 文档编写回调是可重写的。....reconnect()重新连接到数据库 .disconnect()与数据库断开连接 .is_connected()检查我们是否连
在用mysql客户端对数据库进行操作时,打开终端窗口,如果一段时间没有操作,再次操作时,常常会报如下错误: ERROR 2013 (HY000): Lost connection to MySQL server during query ERROR 2006 (HY000): MySQL server...
5、支持H264/H265+AAC组合Rtmp流,支持同步TS\MP4文件录制。 6、支持渲染时保证画面宽高比而自适应加黑边。 7、支持外层可设置的Jitter Buff延时,设置为0时为极速模式,配合低延时推送端最小延时仅500ms。 8、仅六...
pip install django_db_reconnect 注意仅支持pymysql,使django使用pymysql需要先安装包并在settings.py所在目录的__init__.py增加如下代码: import pymysql pymysql.install_as_MySQLdb() 使用 添加django_db_...
reconnect=true¤tschema=yuto #jdbc.driver=com.sap.db.jdbc.Driver #jdbc.url=jdbc:sap://port:30015?reconnect=true¤tschema=CUSTOMIZE #jdbc.username=**** #jdbc.password=***** #hibernate.dialect...
con1->reconnect = 1; mysql_select_db(con1, DBName) ; //mysql_query(con1, "useUnicode=true&characterEncoding=utf-8"); mysql_query(con1,"set names gbk"); if (!mysql_query(con1, "SELECT * FROM user...
reconnect Tcp断线重连实验