我的应用程序由一个使用 redis 调用服务的 Spring rest Controller 组成。 我正在使用 spring boot starter redis 1.2.5,我在 beans.xml 文件中定义了一个模板:
<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>
当我启动超过 8 个查询时,我的应用程序会被阻止。我了解我已达到池中的默认连接数。
为什么在请求处理结束时不自动返回连接?
如何在事务模式下工作,以便任何传入请求都将获得其 redis 连接并在处理结束时返回它?
最佳答案
您需要通过提供 PlatformTransactionManager
bean 为您的应用程序启用事务管理。
最简单的方法是将 @EnableTransactionManagement
添加到您的 Spring Boot 应用程序。如果那不可能,请配置一个 PlatformTransactionManager
bean。重用现有的 DataSourceTransactionManager
是最简单的方法。如果您不使用符合 JDBC 的数据库,只需放入 H2 内存数据库即可。
如果您想使用 JTA 事务管理器,请参阅此博客文章:https://spring.io/blog/2011/08/15/configuring-spring-and-jta-without-full-java-ee/
HTH,马克
关于java - Spring Jedis 连接未返回池,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33493791/