使用的是数据库连接池commons-dbcp-1.3.jar,在程序运行一段时间后,执行下面的语句抛出异常:
if (null != conn) try { conn.close(); } catch (Exception ex) {ex.printStackTrace();}
异常信息:
java.sql.SQLException: Already closed.
....
ERROR [09:36:31 26-三月-17 CST][servlet.alarm] BAlarmServlet.java-initPointMap: error = The last packet successfully received from the server was 128,406,334 milliseconds ago. The last packet sent successfully to the server was 128,406,334 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.
ERROR [09:36:31 26-三月-17 CST][servlet.alarm] BAlarmServlet.java-getAlarmOneDay: The last packet successfully received from the server was 128,406,355 milliseconds ago. The last packet sent successfully to the server was 128,406,355 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.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 128,406,355 milliseconds ago. The last packet sent successfully to the server was 128,406,355 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.
判断为null,关闭,应该是正常的啊?为什么会这样呢?
不过,看到过的程序里面是不会写后面的ex.printStackTrace();异常输出语句的。
http://blog.csdn.net/pq258280920/article/details/17136335
上面的博文讲的符合我的情况,很好!这时由于服务器那边主动断开了空闲连接造成的。
日志不是说的很清楚了么?连接已经被关闭掉了,你再close就报错了。
的确和日志说的一样。但是,我在获取连接后没有做任何关闭操作的。
为什么就是关闭的呢?
不是什么时候都要用 try
这些关闭,连接数据库的代码可以封装起来!方便操作
我用的数DBCP数据库连接池,连接是从那里面获取的。
之前直接建立连接时是没有这样的问题的,该关闭时就关闭了。
检查了一下代码,里面最大连接数为400,但setMinIdle的值设置为15*1000了。
当时我还注释为15秒,可笑可笑。
这个问题应该和mysql数据库连接超时时间、dhcp模块对超时的配置有关系。
更改后的代码:
// Fixed: 15 idle connections
datasrc.setMinIdle(15);
// 120 seconds
datasrc.setRemoveAbandonedTimeout(120);
datasrc.setRemoveAbandoned(true);