分布式缓存Redis之资源释放

来源:互联网 发布:淘宝pc端怎样装修店铺 编辑:程序博客网 时间:2024/06/06 11:03

写在前面

  本学习教程所有示例代码见GitHub:https://github.com/selfconzrr/Redis_Learning

为什么要关闭连接?

  比如下面一段代码:

for(int i = 2000; i < 3000; i++) {    Jedis jedis = redisDao.getJedis();    jedis.set("user:" + i, jedis.toString());    System.out.println(jedis);    set.add(jedis.toString());}  

  在上面的代码中,当循环到600次的时候,就会报出拿不到连接的错误,而600恰好是配置中的连接个数;并且jedis连接不会在使用结束后进行自动释放(因为报错后,再次请求这个方法,连一个连接都拿不到了)。如果在代码的循环中加入

jedis.close();

  则效果就不一样了,可以循环结束,并且打印的jedis对象是同一个连接地址。

  Jedis 3.0版本,使用jedis.close()方法关闭连接。close()源码如下:

@Override  public void close() {    if (dataSource != null) {      if (client.isBroken()) {        this.dataSource.returnBrokenResource(this);      } else {        this.dataSource.returnResource(this);      }    } else {      client.close();    }  }

  可以看到,如果是从JedisPool取得的Jedis实例(Jedis的dataSource成员不为空,即指向一个JedisPool),会进行相应的归还给JedisPool的操作,如果是单独生成的一个Jedis实例(Jedis的dataSource成员为空),则会直接断开与Redis服务器的连接。

Jedis returnResource使用注意事项:

  不能不管什么情况都一律使用returnResource()。

  原因及案例见:

  http://www.codeweblog.com/jedis-returnresource%E4%BD%BF%E7%94%A8%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9/

  更健壮可靠以及优雅的处理方式如下所示:

    while(true){        Jedis jedis = null;        boolean broken = false;        try {            jedis = jedisPool.getResource();            return jedisAction.action(jedis); //模板方法        } catch (JedisException e) {            broken = handleJedisException(e);            throw e;        } finally {            closeResource(jedis, broken);        }    }    /**     * Handle jedisException, write log and return whether the connection is broken.     */    protected boolean handleJedisException(JedisException jedisException) {        if (jedisException instanceof JedisConnectionException) {            logger.error("Redis connection " + jedisPool.getAddress() + " lost.", jedisException);        } else if (jedisException instanceof JedisDataException) {            if ((jedisException.getMessage() != null) && (jedisException.getMessage().indexOf("READONLY") != -1)) {                logger.error("Redis connection " + jedisPool.getAddress() + " are read-only slave.", jedisException);            } else {                // dataException, isBroken=false                return false;            }        } else {            logger.error("Jedis exception happen.", jedisException);        }        return true;    }    /**     * Return jedis connection to the pool, call different return methods depends on the conectionBroken status.     */    protected void closeResource(Jedis jedis, boolean conectionBroken) {        try {            if (conectionBroken) {                jedisPool.returnBrokenResource(jedis);            } else {                jedisPool.returnResource(jedis);            }        } catch (Exception e) {            logger.error("return back jedis failed, will fore close the jedis.", e);            JedisUtils.destroyJedis(jedis);        }    }

—–乐于分享,共同进步
—–Any comments greatly appreciated
—–诚心欢迎各位交流讨论!QQ:1138517609