mysql - 过时的连接,validationQuery 不修复

标签 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 连接器/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" />

最佳答案

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

对于您注意到的第一个异常:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException,我过去将其包装在 try/catch block 中。在 catch 中,对于那个异常,我重新连接了连接,然后重新发送了查询。知道我不想经常这样做,并且仍然保持打开的连接,我还将默认的 wait_timeout 增加到对我的应用程序合理的值。

参见手册引用:http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_wait_timeout

关于mysql - 过时的连接,validationQuery 不修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9069378/

相关文章:

php - 实体关系

php - 搜索表单突然不显示结果

tomcat - 配置 Tomcat 以检查 CRL 分发点

java - 无法在eclipse中将J2EE项目发布到tomcat

mysql - 无法让 DataImportHandler 在 Tomcat 中为 Solr 工作

mysql - 为什么我的 find 子句不起作用?

PHP:用逗号分隔 MySQL 查询值

mysql - 如何在同一查询中多次引用 TEMPORARY 表?

regex - 无法为 Mod Rewrite/URLRewriteFilter 构建正确的正则表达式。我如何否定一个范围?

spring - Tomcat上部署WAR文件时,应该使用什么路径来定位SQL语句中使用的CSV文件(Load data local infile)