Druid 1.2.3 discard long time none received connection原因及解决方法

异常

discard long time none received connection. , jdbcUrl : jdbc:mysql://******?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false, version : 1.2.3, lastPacketReceivedIdleMillis : 172675

源码

public boolean isValidConnection(Connection conn, String validateQuery, int validationQueryTimeout) throws Exception {
        if (conn.isClosed()) {
            return false;
        } else {
            if (this.usePingMethod) {
                if (conn instanceof DruidPooledConnection) {
                    conn = ((DruidPooledConnection)conn).getConnection();
                }

                if (conn instanceof ConnectionProxy) {
                    conn = ((ConnectionProxy)conn).getRawObject();
                }

                if (this.clazz.isAssignableFrom(conn.getClass())) {
                    if (validationQueryTimeout <= 0) {
                        validationQueryTimeout = 1;
                    }

                    try {
                        this.ping.invoke(conn, true, validationQueryTimeout * 1000);
                        return true;
                    } catch (InvocationTargetException var11) {
                        Throwable cause = var11.getCause();
                        if (cause instanceof SQLException) {
                            throw (SQLException)cause;
                        }

                        throw var11;
                    }
                }
            }

            String query = validateQuery;
            if (validateQuery == null || validateQuery.isEmpty()) {
                query = "SELECT 1";
            }

            Statement stmt = null;
            ResultSet rs = null;

            boolean var7;
            try {
                stmt = conn.createStatement();
                if (validationQueryTimeout > 0) {
                    stmt.setQueryTimeout(validationQueryTimeout);
                }

                rs = stmt.executeQuery(query);
                var7 = true;
            } finally {
                JdbcUtils.close(rs);
                JdbcUtils.close(stmt);
            }

            return var7;
        }
    }
this.usePingMethod为true时会调用ping检查连接,除非抛异常,否则返回true;另一种是select 1方式检查。
if (this.ping != null) {
    this.usePingMethod = true;
}

 该类的构造方法中初始化为true,但是可以通过以下方式配置

public void configFromProperties(Properties properties) {
        if (properties != null) {
            String property = properties.getProperty("druid.mysql.usePingMethod");
            if ("true".equals(property)) {
                this.setUsePingMethod(true);
            } else if ("false".equals(property)) {
                this.setUsePingMethod(false);
            }
        }
}

解决方式

一、启动时增加参数

-Ddruid.mysql.usePingMethod=false

二、Spring Boot启动类增加以下代码

    /* 解决 discard long time none received connection */
    static {
        System.setProperty("druid.mysql.usePingMethod","false");
    }

三、自定义DruidConfig配置类增加

@PostConstruct
public void setProperties(){
    System.setProperty("druid.mysql.usePingMethod","false");
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值