redis在应用中使用连接不释放问题解决
Posted a393060727
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis在应用中使用连接不释放问题解决相关的知识,希望对你有一定的参考价值。
今天测试,发现redis使用的时候,调用的链接一直不释放。后查阅蛮多资料,才发现一个配置导致的。并不是他们说的服务没有启动导致的。
1)配置文件
#redis连接配置===================start========================= # Redis settings redis.host=192.168.10.102 redis.port=6379 redis.pass= redis.maxIdle=1 redis.maxActive=9 redis.maxWait=1000 redis.testOnBorrow=true #redis连接配置===================end=========================
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxActive}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" ref="poolConfig" /> <property name="port" value="${redis.port}" /> <property name="hostName" value="${redis.host}" /> <property name="password" value="${redis.pass}" /> </bean> <bean id="stringSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer" ref="stringSerializer" /> <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/> <property name="enableTransactionSupport" value="true"/> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer" ref="stringSerializer" /> <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/> </bean> </beans>
2)测试例子
写了一个springmvc的controller类,然后调用线程使用连接,出现问题。
DemoMvcController.java
package com.iafclub.demo.web.controller; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.iafclub.baseTools.util.MyDateUtil; @Controller public class DemoMvcController { @Autowired private StringRedisTemplate stringRedisTemplate; /** * 跳转方式3 * */ @RequestMapping("/testRedis.do") public void testRedis(Model model, HttpServletRequest request){ System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); for(int i=0;i<5;i++){ Thread thread = new RedisThread(stringRedisTemplate); thread.setName("线程:" + i); thread.start(); } model.addAttribute("status", "完成"+MyDateUtil.getCurrentDateTimeStr()); System.out.println("完成"); System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); } }
RedisThread.java线程类
package com.iafclub.demo.web.controller; import org.junit.runner.RunWith; import org.springframework.data.redis.core.BoundValueOperations; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.iafclub.baseTools.util.MyDateUtil; @RunWith(SpringJUnit4ClassRunner.class) public class RedisThread extends Thread { private StringRedisTemplate redisTemplate; private String REVERSE_KEY = "batchJob:task_"; public RedisThread(StringRedisTemplate redisTemplate){ this.redisTemplate = redisTemplate; } @Override public void run() { // 其实这里使用了多次,但是使用的也都是一个链接 for(int i=0;i<50;i++){ String value = Thread.currentThread().getName() + "{user:user"+MyDateUtil.getCurrentDateTimeStr()+";name:chenweixian"+System.currentTimeMillis()+"}"; redisTemplate.opsForValue().set(REVERSE_KEY+System.currentTimeMillis(), value); redisTemplate.getConnectionFactory().getConnection().close(); // BoundValueOperations<String, String> opt = redisTemplate.boundValueOps(REVERSE_KEY+System.currentTimeMillis()); // opt.set(value); // System.out.println(opt.get()); } System.out.println("完成"); } }
启动应用,访问链接:http://chenweixian-pc:8480/demo-system/testRedis.do,多刷新几次
出现问题异常:Cannot get Jedis connection
Exception in thread "线程:3" org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:162) at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:251) at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:58) at com.iafclub.demo.web.controller.RedisThread.run(RedisThread.java:26) Caused by: redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool at redis.clients.util.Pool.getResource(Pool.java:50) at redis.clients.jedis.JedisPool.getResource(JedisPool.java:88) at redis.clients.jedis.JedisPool.getResource(JedisPool.java:12) at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:155) ... 3 more Caused by: java.util.NoSuchElementException: Timeout waiting for idle object at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:442) at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:360) at redis.clients.util.Pool.getResource(Pool.java:48) ... 6 more
3)查看链接数
通过客户端工具到服务器去查询当前连接数:当前10个
[[email protected] bin]# ./redis-cli info clients # Clients connected_clients:10 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0
4)分析问题
因为我们设置最初的连接数最大是9个,加上我自己通过客户端访问连接数10个,理论上应该释放才对,这里没有释放,是有问题的。因为这个链接应该是与数据库链接一样,会释放,才能长久。。。
间隔很久访问,依旧是10个。没有释放。一旦有httprequest请求发出来,错误依旧是:没有取到链接。
Exception in thread "线程:3" org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
5)修改配置
经过反复查找属性,最终在配置文件中发现一个配置,是事务处理的,网上查询得知,如果启动了redis中的事务管理,必须使用mul和execute执行后才能生效。而我们这里没有使用这个事务。so去掉这个配置。
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer" ref="stringSerializer" /> <property name="valueSerializer" ref="jdkSerializationRedisSerializer"/> <property name="enableTransactionSupport" value="true"/> </bean>
6)重新测试
重新部署,启动,多次刷新后连接数都没有出现无法获取的异常,很正常。
# Clients connected_clients:1 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0
7)问题解决
总结:这个配置项,需要注意。。
以上是关于redis在应用中使用连接不释放问题解决的主要内容,如果未能解决你的问题,请参考以下文章