java - Jedis - 何时使用 returnBrokenResource()

标签 java exception redis jedis

什么时候我们应该使用这个方法。关于 JedisConnectionException、JedisDataException 或任何 JedisException。据我所知,Jedis 没有好的 API 文档。

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

最佳答案

对于迟到者!

returnBrokenResource()、returnResource() 已弃用。只需在 finally block 中安全地使用 jedis.close()

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.

关于java - Jedis - 何时使用 returnBrokenResource(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17082163/

相关文章:

Java 已安装,但 `echo $JAVA_HOME` 似乎没有产生任何输出

android - SharedPreferences getLong() -> java.lang.String 无法转换为 java.lang.Long。怎么修?

ruby-on-rails - 在 Ruby 中对 Redis 数据集进行分页

redis - 如何在重新启动之前通过 Capistrano 删除特定于 Redis 的应用程序缓存

java - 来自多个数据库的 Hibernate 实体

java.nio.BufferOverflowException 构造 byte[]

java - 如何在网页上查找某个网页元素

android - 在平板电脑上强制关闭和 android.view.InflateException

c++ - 流式异常文章

c# - Redis 声明标记为 DataContract 的类不可序列化?