Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Jedis connection not returned to pool

My app is made of a Spring rest controller calling a service using redis. I am using spring boot starter redis 1.2.5 and I have defined a template in my beans.xml file:

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
    p:host-name="${spring.redis.host}"
    p:use-pool="true" 
    p:port="${spring.redis.port}"
/>

<bean id="redisTemplateForTransaction" class="org.springframework.data.redis.core.RedisTemplate"
    p:connection-factory-ref="jedisConnectionFactory"
    p:keySerializer-ref="stringRedisSerializer"
    p:valueSerializer-ref="jsonRedisSerializerForTransaction"
    p:enableTransactionSupport="true">
    <qualifier value="redisTemplateForTransaction" />
</bean>

When I launch more than 8 queries my app blocks. I understand I have reached the default number of connections in the pool.

Why aren't the connections returned automatically at the end of the request processing ?

How to work in a transactional mode so that any incoming request will get its redis connection and return it at the end of the processing ?

like image 348
mordekhai Avatar asked Nov 03 '15 07:11

mordekhai


1 Answers

You need to enable transaction management for your application by providing a PlatformTransactionManager bean.

The easiest way to do so is adding @EnableTransactionManagement to your Spring Boot application. If that's not possible, configure a PlatformTransactionManager bean. Reusing an existing DataSourceTransactionManager is the easiest way. If you do not use a JDBC-compliant database, simply drop in a H2 in-memory database.

If you want to use a JTA transaction manager, see this blog post: https://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/

HTH, Mark

like image 114
mp911de Avatar answered Nov 10 '22 14:11

mp911de