首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么不能使用tomcat上的jedis池发布jedis资源

为什么不能使用tomcat上的jedis池发布jedis资源
EN

Stack Overflow用户
提问于 2015-11-28 03:34:26
回答 1查看 2.7K关注 0票数 0

使用Tomcat运行一个web应用程序。我用jedis连接到我们的redis服务器。

我使用的每个方法都是在finallay块中调用jedis.close(),但它似乎没有将jedis资源返回到池中。

使用

netstat -atnlp|grep 6379

连接数是死气沉沉的。直到jedis客户端抛出"JedisConnectionException:无法从池中获取资源“。我调试代码。jdeis.close()确实在运行。

我的密码有问题吗?

帮帮我,这已经让我们的服务器关闭了很多次。

这是我的jedis pom conf

代码语言:javascript
复制
<!-- jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.3</version>
</dependency>

猫猫

apache-tomcat-7.0.64

服务器是

Centos 6.5

红丝

v2.8.11

春季版:

3.2.13.RELEASE

这是我的JedisUtils代码:

代码语言:javascript
复制
@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中选择x

然后我发现了24个jedis对象。

然后,我再次在同一个tomcat服务器上调用jedis方法,然后再转储。运行相同的OQL,就可以找到25个jedis对象。

也许这个信息是有用的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-30 08:37:25

在我最后一条评论之后,我怀疑您的代码可能会调用util的setExpire方法。请注意,这是分配资源但只在异常情况下关闭资源的唯一方法,而不是在finally块中。

尝试更改您的实现

代码语言:javascript
复制
@Override
public void setExpire(
                String key,
                int expireTime) {
    Jedis jedis = pool.getResource();
    try {
        jedis.expire(key, expireTime);
    } catch (Exception e) {
        jedis.close();
    }
}

代码语言:javascript
复制
@Override
public void setExpire(
                String key,
                int expireTime) {
    Jedis jedis = pool.getResource();
    try {
        jedis.expire(key, expireTime);
    } finally {
        jedis.close();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33967361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档