Jedis3.0以上版本关闭连接时遇到的坑

在高并发场景业务日志频发出现jedis如下异常信息

redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool
	at redis.clients.util.Pool.returnBrokenResourceObject(Pool.java:103)
	at redis.clients.jedis.JedisPool.returnBrokenResource(JedisPool.java:239)
	at redis.clients.jedis.JedisPool.returnResource(JedisPool.java:255)
	at redis.clients.jedis.JedisPool.returnResource(JedisPool.java:16)
	at com.bb.nb.manager.CacheManagerImpl.doHandler(CacheManagerImpl.java:354)
	at com.bb.nb.manager.CacheManagerImpl.handler(CacheManagerImpl.java:337)
	at com.bb.nb.manager.CacheManagerImpl.redisKeyExistsTime(CacheManagerImpl.java:517)
	at sun.reflect.GeneratedMethodAccessor255.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

不要慌,先检查下你的jedis版本是否是Jedis 3.0及以上,如果是那么检查下你的调用完jedis方法之后关闭连接用的是不是下面两个方法。

注意看下面方法的注释上写了,3.0以后这两个方法已经废弃不用了,使用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()}
   */
  @Deprecated
  public void returnBrokenResource(final T resource) {
    if (resource != null) {
      returnBrokenResourceObject(resource);
    }
  }
  
 /**
   * @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()}
   */
  @Deprecated
  public void returnResource(final T resource) {
    if (resource != null) {
      returnResourceObject(resource);
    }
  }

似乎是在高并发的情况下,上面的这种关闭连接的方式不是很可靠,导致有很多没有正常关闭。

你可能感兴趣的:(Java)