我得到了可怕的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 Tomcat6.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" />发布于 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
发布于 2015-08-27 00:20:07
您的验证查询不正确。删除"select 1“,只保留ping。
https://stackoverflow.com/questions/9069378
复制相似问题