首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >陈旧的连接,validationQuery无法修复

陈旧的连接,validationQuery无法修复
EN

Stack Overflow用户
提问于 2012-01-31 03:12:58
回答 2查看 7.3K关注 0票数 5

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

代码语言:javascript
复制
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 Tomcat6.0.24

这里是如何在server.xml中定义连接的,我们使用tomcat-dbcp来池化连接。

代码语言:javascript
复制
 <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" />
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-31 03:59:14

我可能会检查你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

票数 0
EN

Stack Overflow用户

发布于 2015-08-27 00:20:07

您的验证查询不正确。删除"select 1“,只保留ping。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9069378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档