【发布时间】:2012-02-22 13:43:56
【问题描述】:
我遇到了可怕的 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 雄猫 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" />
【问题讨论】:
-
在什么情况下您会遇到那些陈旧的连接异常?您是否正在发送实时查询,这就是结果?您是在执行查询时刚刚创建 JDBC 连接,还是从连接池中获取它们?
-
每天早上当用户第一次连接到我们的网络应用程序时会发生连接异常。连接来自 Tomcat-DBCP 池。我们可以通过每天重启 Tomcat 来解决这个问题,但这只是掩盖了真正的问题。
-
当您重新启动 Tomcat 时,您所做的只是让连接池重新设置自己。您是否认为解决此问题的方法是 tomcat 配置、mysql 配置或 tomcat-dbcp 池代码更改?...只是为了让我知道从哪个方向寻找答案。
-
你看过这篇文章了吗:stackoverflow.com/questions/15949/…
-
更多背景知识:过去我们使用 connector/J 版本 5.0.8 并遇到过时的连接。当时,我们使用 validationQuery 技巧解决了这个问题。最近我们有理由将连接器/J 升级到 5.1.18,并且旧的连接问题又回来了。更新 Tomcat 或 tomcat-dbcp 不是我们的选择。我已经查看了我能想到的所有可能的 server.xml 和 MySQL 变量,但已经没有想法了。