MySQL数据库连接8小时问题怎么解决

九块腹肌进先生丶 发布于 2017/02/23 15:13
阅读 379
收藏 1

下面是我的报错信息:

com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

 

The last packet successfully received from the server was 10,894 milliseconds ago.  The last packet sent successfully to the server was 2,118 milliseconds ago.

at com.jfinal.plugin.activerecord.Model.find(Model.java:565)

at com.jfinal.plugin.activerecord.Model.findFirst(Model.java:594)

at model.dao.Order.findOrderDetail(Order.java:157)

at controller.mobile.OrderController.findOrderById(OrderController.java:361)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at com.jfinal.aop.Invocation.invoke(Invocation.java:73)

at other.interceptor.GlobalActionInterceptor.intercept(GlobalActionInterceptor.java:19)

at com.jfinal.aop.Invocation.invoke(Invocation.java:67)

at com.jfinal.core.ActionHandler.handle(ActionHandler.java:74)

at com.jfinal.core.JFinalFilter.doFilter(JFinalFilter.java:72)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

at demo1.Kuayu.doFilter(Kuayu.java:43)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)

at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)

at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)

at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)

at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:745)

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

 

The last packet successfully received from the server was 10,894 milliseconds ago.  The last packet sent successfully to the server was 2,118 milliseconds ago.

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:526)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)

at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)

at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3715)

at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3604)

at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4155)

at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)

at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)

at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)

at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)

at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)

at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)

at com.jfinal.plugin.activerecord.Model.find(Model.java:546)

at com.jfinal.plugin.activerecord.Model.find(Model.java:563)

... 31 more

Caused by: java.net.SocketException: Connection reset

at java.net.SocketInputStream.read(SocketInputStream.java:196)

 

at java.net.SocketInputStream.read(SocketInputStream.java:122)

at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:112)

at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:159)

at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:187)

at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3158)

at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3615)

... 41 more

加载中
1
北望天狼SOHO
北望天狼SOHO

首先设置mysql的如下参数:

vi /etc/mysql/my.cnf
[mysqld]
wait_timeout=28800
interactive_timeout=28800

重启mysql

sudo stop mysql
sudo start mysql

 然后连接mysql时加上autoReconnect="true"参数

DbConn
URL_TEMPLATE = "jdbc:mysql://%s:3306/%s?autoReconnect=true";

这样会自动重连,但重连之前的那一次连接仍然会失败,可以增加类似下面的代码,在超时时,调用一次无用的mysql操作

private static long sLastDatabaseAccessTime;
private static int VALIDATE_TIMEOUT = 3600 * 1000;

static {
    sLastDatabaseAccessTime = System.currentTimeMillis();
}

public static boolean validateQuery(Connection conn) {
    if ((System.currentTimeMillis() - sLastDatabaseAccessTime) 
            < VALIDATE_TIMEOUT) {
        return true;
    }
    
    ResultSet rs = null;
    Statement stmt = null;
    String sql = "select 1";
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
        if (rs.next()) {
            return true;
        }
        return false;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        System.out.println("finally close Statement and ResultSet");
        sLastDatabaseAccessTime = System.currentTimeMillis();
        DbConn.close(stmt);
        DbConn.close(rs);   
    }
    return false;
}

 

0
OSC_RTOlLX
OSC_RTOlLX
使用数据库连接池
0
zigzagroad
zigzagroad
把应用系统中的数据库连接心跳检查时间设置为小于数据库连接空闲时间即可。
0
f
freezingsky

1,调整默认连接时间 

2.缺少连接池对连接有效性的时长 。

返回顶部
顶部