Redis连接池连接未正常释放

 Caused by: redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
    at redis.clients.util.Pool.getResource(Pool.java:51)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
    at redis.clients.jedis.JedisPool.getResource(JedisPool.java:16)
    at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:194)
    ... 71 common frames omitted


前几天在线上碰到了一个奇怪的问题,jedis 突然无法从连接池取到资源,经过排查发现是因为使用分布式锁的时候,没有释放资源

redisTemplate 和 jedis 不同

redisTemplate 自己实现了资源的释放,不需要像jedis一样手动释放

public boolean setNX(final String key, final String value) throws RedisException {
        return redisTemplate.execute(new RedisCallback<Boolean>() {
            @Override
            public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException {
                return redisConnection.setNX(key.getBytes(), value.getBytes());
            }
        });
    }

所以实现分布式锁的时候需要调用redisTemplate.executeredisTemplate 帮我们释放资源,具体的可以看redisTemplate 的源码。

    原文作者:Aries_Li
    原文地址: https://www.jianshu.com/p/10010592f067
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞