Search code examples
spring-bootjedisspring-data-redisspring-cache

Cannot get Jedis connection, nested exception: Could not get a resource from the pool


I need some help regarding some issues I encounter when trying to connect to Redis using Spring Boot.

I am using the following RedisConfiguration:

@Component
@Configuration
public class SpringRedisConf extends CachingConfigurerSupport {
    
    @Value("${spring.redis.host}")
    private String redisHostName;      
    
    @Bean
    public JedisConnectionFactory redisConnectionFactory() {
        
        JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
        connectionFactory.setHostName("localhost");
        connectionFactory.setUsePool(true);
        connectionFactory.setPort(PortName);
        connectionFactory.getPoolConfig().setMaxTotal(10);
        connectionFactory.getPoolConfig().setMaxIdle(10);
        return connectionFactory;
    }
    
    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        if (null == redisConnectionFactory) {
            LOG.error("Redis Template Service is not available");
            return null;
        }
    
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer( new GenericToStringSerializer< Object >( Object.class ) );
        template.setEnableTransactionSupport(true);
        return template;
    }

        
    @Bean
    public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
        RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
        // Number of seconds before expiration. Defaults to unlimited (0)
        return cacheManager;
    }

And the following class where I am trying o create a unit tests to test my connection:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserApp.class)
public class RedisConnectionTest {

@Autowired
    private RedisTemplate<String, Object> redisTemplate;

@Test
    public void testRedisConnection() {      
         redisTemplate.opsForValue().set("mouse", "loves cheese");
         assertThat( redisTemplate.opsForValue().get( "mouse").equals("loves cheese"));

    }
}

I know there is a similar question here, but I do not have yet the required score to comment, I have tried their suggestions and I still have the same result. Here is my pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>${redis_clients_version}</version>
</dependency>

And here is my properties file:

spring.data.redis.repositories.enabled=true
spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=XXXX
spring.cache.redis.time-to-live=0ms
spring.cache.redis.cache-null-values=false

The StackTrace is this:

   redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
            at java.net.PlainSocketImpl.socketConnect(Native Method)
            at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
            at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
            at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
            at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
            at java.net.Socket.connect(Socket.java:589)
            at redis.clients.jedis.Connection.connect(Connection.java:184)



        

Any idea what am I doing wrong? Thanks


Solution

  • Your port numbers are misconfigured between properties and the self-configured connection factory.

    I would recommend sticking with one or the other Spring Boot's RedisAutoconfiguration which pulls in JedisConnectionConfiguration will automatically create a ConnectionFactory for you based of the properties.

    https://github.com/spring-projects/spring-boot/blob/8f4bf233b4895a4fade5aff41e0a309f90ba3193/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java

    Driven from the spring.redis properties.