druid discard long time none received connection问题解析

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

druid discard long time none received connection问题解析

天冰   2021-01-21 我要评论

最新项目中用的druid连接数据库遇到一个困扰很久的问题

1 开始用的druid版本是1.1.22版本,由于业务需求,单个连接需要执行很久,理论上不需要用到自动回收,但为了安全,还是加了自动回收,时间设置的2个小时。

问题来了,程序经常报The last packet successfully received from the server was XXXXX milliseconds ago.  The last packet sent successfully to the server was 0 mill
iseconds ago错误,网上搜索了下答案,有说配置项,改数据库事件设置,试过都没有解决,后续看到https://cloud.tencent.com/developer/article/1397508 分析,觉得有一定道理,就开始后续之路

2.druid包升级到1.2.2,原来的问题是没有了,新的问题出现了,discard long time none received connection,又继续网上搜索答案,出来的结果一塌糊涂,很多说版本回退到1.1.22,心里不由的说wc,这...

有点扯,继续进行搜索测试,修改配置项validationQuery,修改testWhileIdle,修改...继续测试,问题依旧,又搜索到运行时添加druid.mysql.usePingMethod=false,但是没说怎样添加,没办法下载源码进行查看,导入源码后发现如下:

 if (valid && isMySql) { // unexcepted branch
                    long lastPacketReceivedTimeMs = MySqlUtils.getLastPacketReceivedTimeMs(conn);
                    if (lastPacketReceivedTimeMs > 0) {
                        long mysqlIdleMillis = currentTimeMillis - lastPacketReceivedTimeMs;
                        if (lastPacketReceivedTimeMs > 0 //
                                && mysqlIdleMillis >= timeBetweenEvictionRunsMillis) {
                            discardConnection(holder);
                            String errorMsg = "discard long time none received connection. "
                                    + ", jdbcUrl : " + jdbcUrl
                                    + ", version : " + VERSION.getVersionNumber()
                                    + ", lastPacketReceivedIdleMillis : " + mysqlIdleMillis;
                            LOG.warn(errorMsg);
                            return false;
                        }
                    }
                }

这在配置中加timeBetweenEvictionRunsMillis:1800000 就可以了,个人理解是一次操作数据库大于这个时间就会被清除,更直观些就是查询或其他操作在数据库执行时间,这里单位是毫秒。

紧接着查看源码druid.mysql.usePingMethod=false这个设置,既然网上有人说,就看看好使不,源码如下:

 configFromProperties(System.getProperties());
    }

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

druid加载System.getProperties(),查看属性中的druid.mysql.usePingMethod的对应值,如果false,就不用ping方法,否者用ping方法,进一步查看不用ping方法就是用默认select 1,System.getProperties()查看了下一般是系统的一些参数,但是可以put(key,value),程序启动时间加载进去就可以,项目中用到了定时器(根据自己项目写就可以,加载一次就ok了),就在初始化时间设置了具体值,代码如下:

public void contextInitialized(ServletContextEvent arg0) {
        try {
            System.getProperties().put("druid.mysql.usePingMethod", "false");
            // 获取Scheduler实例
            scheduler = new StdSchedulerFactory().getScheduler();
...

然后取消timeBetweenEvictionRunsMillis设置进行测试,程序跑1个小时没有任何问题,到此问题解决。

druid个人使用总结:

1.The last packet successfully received from the server was问题升级jar包,我是升级到1.2.2版本

2.discard long time none received connection问题不该程序情况下设置timeBetweenEvictionRunsMillis参数(注意是毫秒),改程序下加System.getProperties().put("druid.mysql.usePingMethod", "false")

druid默认使用usePingMethod方法,此方法并不会更新连接返回时间,导致lastPacketReceivedTimeMs大于timeBetweenEvictionRunsMillis

网上其他的方法感觉要不理解太深,没有给出具体实现,要不就是复制粘贴的,希望对遇到此问题的人有所帮助。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们