Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool

I am getting error while trying to returnResources from the jedis pool.The code are as follow.

Jedis publisherJedis = jedispool.getResource();
if(!redisPassword.equals(""))
    publisherJedis.auth(redisPassword);
publisherJedis.publish(channel,data);
log.debug("Publisher jedis is connected: " + publisherJedis.isConnected());
log.debug("Jsondata is added into the queue " + data);
try {
    jedispool.returnResource(publisherJedis);
    jedispool.destroy();
    publisherJedis.close();
} catch (Exception e) {
    e.printStackTrace();
    log.error("Exception occured in returing resource " + e);
}
like image 215
Kundan Ray Avatar asked Oct 26 '25 22:10

Kundan Ray


1 Answers

I was closing the redis client elsewhere in my application that's why when I was going to close redis client then it was throwing exception.Also I have noted that we should be more careful while using redis client. If we get the resource from redis pool then we will also must have to disconnect them after using it.If we are not doing this then client will be inreasing and after meeting maxSizeClient limit it will also throw exception.I made changes in the start method of Publisher.

public void start(JedisPool jedispool, Jedis publisherJedis, String channel,String data, String redisPassword)
{
    if(!redisPassword.equals(""))
        publisherJedis.auth(redisPassword);
    publisherJedis.publish(channel,data);
    log.debug("Jsondata is added into the queue " +data);
    try{
        publisherJedis.close();
        log.debug(" Is Jedis connected " +publisherJedis.isConnected());
        if(publisherJedis.isConnected())
            publisherJedis.disconnect();
        log.debug(" After disconnecting: is redis connected  " +publisherJedis.isConnected());
    }catch(Exception e){
        log.debug("Error occured " +e);
    }
}
like image 50
Kundan Ray Avatar answered Oct 29 '25 13:10

Kundan Ray