陈旧的连接,validationQuery无法修复

时间:2012-01-30 19:12:58

标签: mysql tomcat

我遇到了可怕的MySQL JDBC陈旧连接异常:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 243,263,541 milliseconds ago.  The last packet sent successfully to the server was 243,263,541 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.

似乎每个人都同意这是通过使用validationQuery + testOnBorrow来解决的,但这并没有解决问题。

我正在使用以下软件 MySQL 5.1.41-3ubuntu12.10 Connector / J 5.1.18 Tomcat 6.0.24

以下是如何在server.xml中定义连接,我们使用tomcat-dbcp来汇集连接。

 <Resource
       auth="Container"
       driverClassName="com.mysql.jdbc.Driver"
       factory="org.apache.commons.dbcp.BasicDataSourceFactory"
       logAbandoned="true"
       maxActive="75"
       maxIdle="20"
       maxWait="10000"
       name="jdbc/jndiname"
       password="password"
       removeAbandoned="true"
       removeAbandonedTimeout="60"
       validationQuery="/* ping */SELECT 1"
       testOnBorrow="true"
       testOnReturn="true"
       timeBetweenEvictionRunsMillis="10000"
       testWhileIdle="true"
       scope="Shareable"
       type="javax.sql.DataSource"
       url="jdbc:mysql://host:3306/schema"
       username="username" />

3 个答案:

答案 0 :(得分:0)

我可能会检查my.cnf文件中的wait_timeout。默认值为28800秒或8小时。它的最大值为31536000秒或365天。

对于您注意到的第一个例外:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException,我过去曾在try / catch块中包含它。在catch中,对于该异常,我重新连接了连接,然后重新发送查询。在知道我不想经常这样做并仍然保持开放连接的同时,我还将默认的wait_timeout增加到了我的应用程序的合理范围。

请参阅手册参考:http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_wait_timeout

答案 1 :(得分:0)

您的验证查询不正确。删除&#34;选择1&#34;并且只留下ping。

答案 2 :(得分:0)

my.cnf的{​​{1}}中,将以下属性设置为365天的大值 -

mysql
wait_timeout = 31536000

会话interactive_timeout = 31536000值将使用非交互式连接的全局wait_timeout值进行初始化,并使用全局wait_timeout值进行交互式连接。

PS - 两个值都以秒为单位。