jedis icon indicating copy to clipboard operation
jedis copied to clipboard

JedisPool getResource block in multi-thread

Open lonnyzhang423 opened this issue 4 years ago • 4 comments

Here is my pool config

GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(8);
config.setMaxIdle(8);
config.setMinIdle(0);
config.setBlockWhenExhausted(true);
config.setMaxWaitMillis(-1);
jedisPool = new JedisPool(config, "localhost", 6379);

My test code

ExecutorService service = Executors.newCachedThreadPool();
service.submit(() -> {
    while (true) {
        int active = jedisPool.getNumActive();
        int idle = jedisPool.getNumIdle();
        int waiters = jedisPool.getNumWaiters();
        System.out.println(String.format("[PoolMonitor] active:%s idle:%s waiters:%s", active, idle, waiters));
        ThreadUtil.sleepSilently(1000);
    }
});
AtomicLong l = new AtomicLong(0);
for (int i = 0; i < 10; i++) {
    service.submit(() -> {
        try {
            while (true) {
                try (Jedis jedis =jedisPool.getResource()) {//BLOCK HERE
                    System.out.println("count:" + l.incrementAndGet());
                    jedis.get("abc");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}

Test result

count:5161
count:5162
count:5163
count:5164
count:5165
count:5166
count:5167
count:5168
count:5169
[PoolMonitor] active:8 idle:0 waiters:10
[PoolMonitor] active:8 idle:0 waiters:10
[PoolMonitor] active:8 idle:0 waiters:10
[PoolMonitor] active:8 idle:0 waiters:10

Why jedisPool.getResource() block and never return?

lonnyzhang423 avatar Aug 22 '19 06:08 lonnyzhang423

@lonnyzhang423 Put a positive value in setMaxWaitMillis.

sazzad16 avatar Aug 24 '19 05:08 sazzad16

@sazzad16 Thanks for your reply. But as for me,I need to block forever when the pool exhausted. I wonder why jedis instance stay active and not be returned to the pool since pool is thread safe and get("abc") will not block for long time?

lonnyzhang423 avatar Sep 04 '19 01:09 lonnyzhang423

Having this problem also, how do we free the resources?

bubbajoe avatar May 08 '22 14:05 bubbajoe

This issue is marked stale. It will be closed in 30 days if it is not updated.

github-actions[bot] avatar Feb 15 '24 00:02 github-actions[bot]