redis.clients.jedis.exceptions.JedisException:无法将资源返回到池中

时间:2015-02-03 13:56:27

标签: java multithreading jedis

我在尝试从jedis池返回资源时遇到错误。代码如下。

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);
}

1 个答案:

答案 0 :(得分:2)

我正在关闭我的应用程序中其他地方的redis客户端,这就是为什么当我要关闭redis客户端然后它抛出异常时。我也注意到在使用redis客户端时我们应该更加小心。如果我们从redis池获取资源,那么我们也必须在使用它之后断开它们。如果我们没有这样做,那么客户端将是inreasing并且在满足maxSizeClient限制之后它也将抛出异常。我在start方法中进行了更改出版商。

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);
    }
}