zoukankan      html  css  js  c++  java
  • MySQL自动关闭连接导致DBCP报错


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

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

    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 omitted

    原因是DBCP保持连接的超时时间比MySQL连接超时时间长。mysql配置中的wait_timeout值一定要大于等于连接池的idle_timeout值,否则mysql会在wait_timeout的时间后关闭连接,然而连接池还认为该连接可用,这样就会产生异常。

    通过MySQL客户端连到服务器,查看目前的配置。


    参考资料

    1.      DBCP配置

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

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

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

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

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

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


  • 相关阅读:
    poj 2262
    poj 1050
    poj 1730
    poj 1061
    【设计模式】简单工厂模式学习
    【待学】
    [设计模式]策略模式和单一职责
    Windows live writer 误删 草稿 恢复
    [Linq]Linq To Sql (2)
    MVC 学习日志1(上)
  • 原文地址:https://www.cnblogs.com/xiaomaohai/p/6157789.html
Copyright © 2011-2022 走看看