写在前面
本学习教程所有示例代码见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