Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jedis - When to use returnBrokenResource()

When exactly we should use this method. On JedisConnectionException, JedisDataException or for any JedisException. There is no good API documentation for Jedis to my knowledge.

try {
    Jedis jedis = JedisFactory.getInstance();
    Pipeline pipe = jedis.pipelined();
    Response<Set<Tuple>> idWithScore = pipe.zrangeWithScores(cachekey, from, to);
    **// some statement which may cause some other exception**
    Response<String> val = pipe.get(somekey);
    pipe.exec();
    pipe.sync();
}catch (JedisConnectionException e) {
    JedisFactory.returnBrokenResource(jedis);
}catch(Exception e){
    **// What API I should use here?, how to find whether to use returnBrokenResource(jedis) or returnResource(jedis)**
}finally{
    JedisFactory.returnResource(jedis);
}
like image 855
user1182253 Avatar asked Jun 13 '13 08:06

user1182253


2 Answers

For latecomers!

returnBrokenResource(), returnResource() are deprecated. Just use jedis.close() in finally block safely.

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.

like image 133
kann Avatar answered Nov 05 '22 18:11

kann


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).

like image 10
Didier Spezia Avatar answered Nov 05 '22 18:11

Didier Spezia