jedis
jedis copied to clipboard
JedisPool getResource block in multi-thread
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 Put a positive value in setMaxWaitMillis
.
@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?
Having this problem also, how do we free the resources?
This issue is marked stale. It will be closed in 30 days if it is not updated.