MySQL自动关闭链接致使DBCP报错


长时间不操做页面后,再操做时程序就会报错,异常以下:html

 

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failurejava

Thelast packet successfully received from the server was 8,064,672 millisecondsago. The last packet sent successfully to the server was 8,064,672 millisecondsago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
atsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3317)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1941)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2114)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2696)
atcom.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2105)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2264)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
atorg.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
atorg.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
...
Caused by: java.net.SocketException: Connection reset by peer: socket writeerror
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3298)
... 34 common frames omittedmysql

 

缘由是DBCP保持链接的超时时间比MySQL链接超时时间长。mysql配置中的wait_timeout值必定要大于等于链接池的idle_timeout值,不然mysql会在wait_timeout的时间后关闭链接,然而链接池还认为该链接可用,这样就会产生异常。sql

 

经过MySQL客户端连到服务器,查看目前的配置。数据库


 

参考资料apache

 

1.      DBCP配置服务器

http://commons.apache.org/dbcp/configuration.htmlsocket

http://wenku.baidu.com/view/06c94707de80d4d8d15a4f9a.html.net

 

2.      MySQL链接空闲超过8小时DBCP形成的异常如何解决server

http://database.51cto.com/art/201107/276325.htm

 

3.      MySQL数据库链接超时(wait_timeout)问题的处理

http://sarin.iteye.com/blog/580311