分布式缓存Redis之资源释放

写在前面

  本学习教程所有示例代码见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);
	    }
	}

------至所有正在努力奋斗的程序猿们!加油!!
有码走遍天下 无码寸步难行
1024 - 梦想,永不止步!
爱编程 不爱Bug
爱加班 不爱黑眼圈
固执 但不偏执
疯狂 但不疯癫
生活里的菜鸟
工作中的大神
身怀宝藏,一心憧憬星辰大海
追求极致,目标始于高山之巅
一群怀揣好奇,梦想改变世界的孩子
一群追日逐浪,正在改变世界的极客
你们用最美的语言,诠释着科技的力量
你们用极速的创新,引领着时代的变迁

——乐于分享,共同进步,欢迎补充
——Any comments greatly appreciated
——诚心欢迎各位交流讨论!QQ:1138517609
——GitHub:https://github.com/selfconzrr

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BugFree_张瑞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值