Home > Software engineering >  Could not get a resource from the pool - Error when connecting with redis docker container using Jav
Could not get a resource from the pool - Error when connecting with redis docker container using Jav

Time:06-15

My Docker File:

version: "3.8"

services:
  cache:
    image: redis:latest
    container_name: local-redis
    restart: always
    ports:
      - '6379:6379'
    command: redis-server --save 20 1 --loglevel warning --requirepass thisismypassword
    volumes:
      - cache:/data

volumes:
  cache:
    driver: local

My code to connect with the redis container:

JedisPoolConfig poolConfig = new JedisPoolConfig(); JedisPool jedisPool = new 
JedisPool(poolConfig, "http://localhost", 6379, Protocol.DEFAULT_TIMEOUT, "thisismypassword");
Jedis jedis = jedisPool.getResource();

I still get the exception

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

Some stackoverflow articles suggest that the redis server may not be running, which does not seem to be the case because trying to connect with the below code succeeds and I can perform any operation:

Jedis jedis = new Jedis("http://localhost:6379");
jedis.auth("thisismypassword");

However, I do not want to do this since below constructor is deprecated

Jedis jedis = new Jedis("http://localhost:6379");

CodePudding user response:

You would have to send the hostname or host address for the concerned parameter. http://localhost is neither of them. Just use localhost for the value of that parameter.

So,

JedisPool jedisPool = new JedisPool(poolConfig, "localhost", 6379, Protocol.DEFAULT_TIMEOUT, "thisismypassword");
  • Related