java - 为什么不能在tomcat上使用jedis池释放jedis资源

标签 java tomcat redis resources jedis

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

我使用的每个方法都是在 finallay block 中调用 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>

tomcat是

apache-tomcat-7.0.64

服务器是

Centos 6.5

redis 是

v2.8.11

Spring 版:

3.2.13.RELEASE

这是我的 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:

select x from redis.clients.jedis.Jedis x

然后我找到了24个jedis对象。

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

也许此信息有帮助。

最佳答案

在我最后一条评论之后,我怀疑您的代码可能会调用您的实用程序的 setExpire 方法。请注意,这是唯一一个您分配资源但仅在出现异常时关闭它的资源,而不是在 finally block 中。

尝试改变你的实现

@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();
    }
}

关于java - 为什么不能在tomcat上使用jedis池释放jedis资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33967361/

相关文章:

java - 如何找到 foreach 循环的迭代次数?

Java流收集计数到字段

tomcat - ColdFusion 10 + IIS : Non-existant URLs that are CFM files. 执行 404 页面后检索原始 URL

Redis节点不会进入MASTER模式

node.js - Node.js、Redis、套接字

java - HttpURLConnection:发送 10,000 个请求给出 "Connection reset"

java - 从另一个类 Android Studio 访问变量

java - 第一个 tomcat 应用程序 - 'ant install' 错误 "NoClassDefFound"

java - 在 Tomcat 上配置 SQL Server 连接池

redis - 使用 Redis Sorted Sets 实现 Dense Rank