I'm running multiple worker threads(around 10) to access the data from the redis Q.
For the i'm using infinte timeout for Jedis Client.
Jedis jedis = pool.getResource();
jedis.getClient().setTimeoutInfinite();
Still i'm getting the error "Could not get a resource from the pool". The stacktrace is given below.
redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
at Workers.Worker1.met1(Worker1.java:124)
at Workers.Worker1.work(Worker1.java:108)
at org.gearman.impl.worker.WorkerConnectionController$3.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketTimeoutException: connect timed out
at redis.clients.jedis.Connection.connect(Connection.java:124)
at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:54)
at redis.clients.jedis.BinaryJedis.connect(BinaryJedis.java:1657)
at redis.clients.jedis.JedisPool$JedisFactory.makeObject(JedisPool.java:63)
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1188)
at redis.clients.util.Pool.getResource(Pool.java:20)
... 6 more
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(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:119)
... 11 more
Based on Rick Hanlon's answer, this exception is also thrown if using Redis with Spring Boot.
If you're using Spring Boot, just the dependency of Redis is not enough; you also need to manually download and install Redis on your machine from redis.io, then run it off of a Bash terminal:
After running the server, you'll need to add the relevant lines in all your apps that use Redis:
application.properties
:application.yml
:Possible causes;
1 - Redis server is down or Redis application not responding.
2 - Application can not connect to Redis server (firewall etc. issues).
3 - Connection to Redis server timed out.
4 - All connections in the (Redis) pool are currently busy, new connection can not be allocated.
The cases 1 and 2 are infra related.
For case 3, connection timeout must be increased ("RedisConnectionTimeout"):
For case 4, max connection count must be increased ("RedisMaximumActiveConnectionCount"):
Assuming the following or similar implementation;
I noticed that this exception can and will be thrown if Redis is not running. Just a heads up.
Not sure, but maybe you don't return Jedis objects to the pool, and your redis-server has connection limit.
Every worker thread should return Jedis instances to the pool after its work is complete:
Did it happen always or occasionally? If it was occasional, you may want to check the size of your connection pool.
Default connection pool size, if you are using
JedisPoolConfig
, is 8. This could be too small for your case.If your code is like this:
You can try this:
This is because for Redis the default timeout time is 2 seconds, but the program may have finished running in this time.