Search code examples
redisgarbage-collectiondisposejedis

releasing Jedis pool


In most examples including this Jedis example the Jedis pool is created in the try..catch parenthesis which I think disposes it:

try(Jedis jedis = jedisPool.getResource())
{
    some code
}

For me this is not possible because I need to throw errors while still using the Jedis pool, depending on result from Redis. So for my this is just

Jedis jedis = jedisPool.getResource()

Question is how best to dispose the object? Is it jedis.disconnect? jedis.quit? jedis = null?

There are some similar questions but not exactly same. One about error it gives, another about increasing pool, another about threading.


Solution

  • Simply jedis.close(). See Jedis - When to use returnBrokenResource()

    You used to be expected to use jedisPool.returnBrokenResource(jedis) or jedisPool.returnResource(jedis), but jedis.close() takes care of it.

    See Jedis.java.

    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
    
    ...
    }catch (JedisConnectionException e) {
    ...
    }catch (Exception e){
    ...
    } finally {
        if (jedis != null)
            jedis.close();
    }