[英]WHY Can't release jedis resource using jedis pool on tomcat
使用Tomcat运行Web应用程序。 我使用jedis连接到我们的Redis服务器。
我使用的每个方法都是在finallay块中调用jedis.close(),但看起来好像没有将jedis资源返回到池中。
使用
netstat -atnlp | grep 6379
连接数增加。 直到jedis客户端抛出“ JedisConnectionException:无法从池中获取资源”。 我调试代码。 jdeis.close()已运行。
我的代码有问题吗?
帮帮我,这已经使我们的服务器停机了很多次。
这是我的jedis pom conf
<!-- jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
雄猫是
apache-tomcat-7.0.64
服务器是
Centos的6.5
Redis是
v2.8.11
春季版:
3.2.13发布
这是我的JedisUtils代码:
@Service(value = "redisCacheService")
public class RedisCacheServiceImpl implements CacheService {
/**
* redis Version No.
*/
private static final String VERSION = "000";
private static JedisPool pool;
static {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(500);
config.setMaxIdle(5);
config.setMaxWaitMillis(1000 * 10);
config.setTestOnBorrow(true);
pool = new JedisPool(config, "10.10.65.10", 6379);
}
@Override
public int set(
String key,
String value) {
Jedis jedis = pool.getResource();
try {
return jedis.set(VERSION + "|" + key, value).equals("OK") ? 1 : 0;
} finally {
jedis.close();
}
}
@Override
public int set(
String key,
String value,
int seconds) {
Jedis jedis = pool.getResource();
try {
return jedis.setex(VERSION + "|" + key, seconds, value).equals("OK") ? 1 : 0;
} finally {
jedis.close();
}
}
@Override
public String get(
String key) {
Jedis jedis = pool.getResource();
try {
return jedis.get(VERSION + "|" + key);
} finally {
jedis.close();
}
}
@Override
public int del(
String key) {
Jedis jedis = pool.getResource();
try {
return Integer.valueOf(jedis.del(VERSION + "|" + key).toString());
} finally {
jedis.close();
}
}
@Override
public void setExpire(
String key,
int expireTime) {
Jedis jedis = pool.getResource();
try {
jedis.expire(key, expireTime);
} catch (Exception e) {
jedis.close();
}
}
}
更多信息:2015-11-28 19:58:50
现在,与redis服务器的连接数仍在增长。
使用jmap转储所有堆信息,并在jvisualvm上运行OQL:
从redis.clients.jedis.Jedis中选择x
然后我发现了24个jedis对象。
然后,我在同一个tomcat服务器上再次调用jedis方法,然后再次转储。 运行相同的OQL,找到25个jedis对象。
也许此信息很有帮助。
在我最后的评论之后,我怀疑您的代码可能会调用您的util的setExpire
方法。 请注意,这是分配资源的唯一方法,但只有在发生异常的情况下才会关闭资源,而不是在finally
块中关闭。
尝试更改您的实现
@Override
public void setExpire(
String key,
int expireTime) {
Jedis jedis = pool.getResource();
try {
jedis.expire(key, expireTime);
} catch (Exception e) {
jedis.close();
}
}
至
@Override
public void setExpire(
String key,
int expireTime) {
Jedis jedis = pool.getResource();
try {
jedis.expire(key, expireTime);
} finally {
jedis.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.