Jedis returnBrokenResource returnResource的标准写法

Jedis 3.0 以后(含3.0)

finally {
  if (jedis != null) {
    jedis.close();
  }
}
  • If Jedis was borrowed from pool, it will be returned to pool with proper method since it already determines there was JedisConnectionException occurred. If Jedis wasn't borrowed from pool, it will be disconnected and closed.

 

Jedis 3.0 以前

public String get(String keyName)
{
    Jedis redis = null;
    try
    {
        redis = redisPool.getResource();
        return redis.get(keyName);
    }
    catch (JedisConnectionException e) 
    {
        if (redis != null) 
        {
            redisPool.returnBrokenResource(redis);
            redis = null;
        }
        throw e;
    }
    finally
    {
        if (redis != null)
        {
            redisPool.returnResource(redis);
        }
    }
}
  • You are supposed to use returnBrokenResource when the state of the object is unrecoverable. A Jedis object represents a connection to Redis. It becomes unusable when the physical connection is broken, or when the synchronization between the client and server is lost.
  • With Jedis, these errors are represented by the JedisConnectionException. So I would use returnBrokenResource for this exception, and not the other ones.
  • JedisDataException is more related to bad usage of the Jedis API, or to server-side Redis errors.
  • JedisException is for everything else (usually raised after a lower-level error, independant from Jedis).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值