• jedis异常:NoSuchElementException: Timeout waiting for idle object


    项目发现网络环境:java.util.NoSuchElementException: Timeout waiting for idle object

    问题原因:不应该在try中释放资源。而应该在finally中处理。尽管是非常基础的语法。但还是有可能会写错,维护老系统时发现非常几处都存在这样的潜在的Bug,引以为戒。在try中释放资源,每当出现一次异常将会导致一个jedis对象无法释放,pool池中可用的jedis对象资源会越来越少。终于将会导致java.util.NoSuchElementException: Timeout waiting for idle object。

    这样的问题是一个慢性问题,须要时间积累才会发作。

    因为请求无法获取空暇对象,页面会出现服务端500错误。若代码增加循环获取jedis将还可能出现server宕机。

    try {
    jedis = pool.getResource();
    // xxx 业务代码
    // 原来代码:pool.returnResource(jedis);,应该放在finally块中,否则每次发生异常将导致一个jedis对象没有被t
    } catch (RuntimeException e) { 
             if(jedis != null ) {
                     pool.returnBrokenResource(jedis);
    }
    } finally{ 
            // 正确释放资源
             if(jedis != null ) {
                    pool.returnResource(jedis);
             }
    }

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    [leetcode] Delete Operation for Two Strings
    [leetcode] Minimum ASCII Delete Sum for Two Strings
    [leetcode] Palindromic Substrings
    [leetcode] Student Attendance Record I
    [leetcode] Reverse String II
    [leetcode] Diameter of Binary Tree
    [leetcode] Climbing Stairs
    [leetcode] Range Sum Query
    Codeforces 1294A Collecting Coins
    团体程序设计天梯赛 L2-021 点赞狂魔 (25分)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4742983.html
Copyright © 2020-2023  润新知