jedis - Could not get a resource from the pool

6,173 views
Skip to first unread message

박종현

unread,
Nov 15, 2016, 1:59:03 AM11/15/16
to Redis DB
Hi, I'm using Jedis from a multi threaded environment and I'm getting this error code

Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:53)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
at HelloRedis.<init>(HelloRedis.java:22)
at HelloRedis.main(HelloRedis.java:38)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.ConnectException: Connection refused: connect
at redis.clients.jedis.Connection.connect(Connection.java:207)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:93)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1767)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:106)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:868)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:435)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 3 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at redis.clients.jedis.Connection.connect(Connection.java:184)
... 10 more



Here is my Jedis test code.


import java.util.ArrayList;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class HelloRedis implements Runnable{
int seq;
Jedis jedis;

static JedisPool pool;
static JedisPoolConfig config;
String str = "@";
        public HelloRedis(int seq){
this.seq = seq;
this.jedis = pool.getResource();
}
public static void main(String args[]) {
config = new JedisPoolConfig();
config.setMaxTotal(1000000);
config.setMaxIdle(1000000);
pool = new JedisPool(config, "localhost", 6379, 0);

long startTime = System.currentTimeMillis(); 

ArrayList<Thread> threads = new ArrayList<Thread>();
for(int i=0; i<100000; i++) {
                      Thread t = new Thread(new HelloRedis(i));
                      t.start();
                      threads.add(t);
                }
for(int j = 0; j<threads.size(); j++){
Thread t = threads.get(j);
try{
t.join();
}catch(Exception e){
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis(); 
System.out.println(":::::::::"+(endTime-startTime));
}

@Override
public void run() {

for(int i=0; i < 10000; i++){
jedis.set(seq+"key"+i, i+str);

}

jedis.close();
}
}



I set the number of maxclients is 10,000,000 in redis.conf file. 

I wonder why this code occur error although I set that Jedis pool size is bigger than the number of threads.

I'm using Redis 3.2.1, Jedis 2.9.0 and commons-pool2 2.4.2

CharSyam

unread,
Nov 15, 2016, 3:04:47 AM11/15/16
to redi...@googlegroups.com
Did you run redis server?
> --
> You received this message because you are subscribed to the Google Groups
> "Redis DB" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to redis-db+u...@googlegroups.com.
> To post to this group, send email to redi...@googlegroups.com.
> Visit this group at https://groups.google.com/group/redis-db.
> For more options, visit https://groups.google.com/d/optout.

박종현

unread,
Nov 15, 2016, 3:24:24 AM11/15/16
to Redis DB
Yes. I run redis server in windows

2016년 11월 15일 화요일 오후 5시 4분 47초 UTC+9, CharSyam 님의 말:

CharSyam

unread,
Nov 15, 2016, 4:05:44 AM11/15/16
to redi...@googlegroups.com
Please check like below.
1] just try to connect your redis server, is it OK?

2] set below and test again.
1. set maxclient just as 1024 for test
2. set config.setMaxTotal(100);
3. set config.setMaxIdle(100);
Message has been deleted
Message has been deleted

박종현

unread,
Nov 15, 2016, 4:29:52 AM11/15/16
to Redis DB
Thank you! It runs well when I set 2]. 

By the way, why did this code occur the error when maxclient, setMaxTotal and setMaxIdle are high values?

2016년 11월 15일 화요일 오후 6시 5분 44초 UTC+9, CharSyam 님의 말:

CharSyam

unread,
Nov 15, 2016, 4:48:50 AM11/15/16
to redi...@googlegroups.com
It depens on operating systems. :)
Because of Open File Descriptor or just over spending of socket(you
can just use maximum 65535 clients)
Reply all
Reply to author
Forward
0 new messages