Jedis3.0版本后jedisPool.returnResource()遭弃用,官方重写了Jedis的close方法用以代替
官方重写了redis的close(),可通过以下代码完成回收:
try {
    jedis = pool.getResource();
} finally {
    if (jedis != null) {
        jedis.close();
    }
}
代码如下:
/**
   * @deprecated starting from Jedis 3.0 this method will not be exposed.
   * Resource cleanup should be done using @see {@link redis.clients.jedis.Jedis#close()}
   */
  @Override
  @Deprecated
  public void returnResource(final Jedis resource) {
    if (resource != null) {
      try {
        resource.resetState();
        returnResourceObject(resource);
      } catch (Exception e) {
        returnBrokenResource(resource);
        throw new JedisException("Could not return the resource to the pool", e);
      }
    }
  }

官方建议应用redis.clients.jedis#Jedis的close方法进行资源回收,官方代码如下:
@Override
  public void close() {
    if (dataSource != null) {
      if (client.isBroken()) {
        this.dataSource.returnBrokenResource(this);
      } else {
        this.dataSource.returnResource(this);
      }
    } else {
      client.close();
    }
  }
以上为替代方案。
评论 (0)