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个
[root@dev2 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:
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
7)问题解决
总结:这个配置项,需要注意。。
redis在应用中使用连接不释放问题解决的更多相关文章
- ConnectionPool实现redis在python中的连接
这篇文章主要介绍了Python与Redis的连接教程,Redis是一个高性能的基于内存的数据库,需要的朋友可以参考下 今天在写zabbix storm job监控脚本的时候用到了python的re ...
- Redis安装 java中的连接 序列化 反序列化
安装路径 /webapp/redis/redis- #启动redis /webapp/redis/redis-/src/redis-server & #关闭redis /webapp/redi ...
- Redis】Java中使用Jedis操作Redis(Maven导入包)、创建Redis连接池
如果我们使用Java操作Redis, 需要确保已经安装了 redis 服务及 Java redis 驱动. Maven项目可以直接在pom.xml中加入jedis包驱动: <!-- https: ...
- redis 在 php 中的应用(Connection [ 连接] 篇)
本文为我阅读了 redis参考手册 之后编写,注意 php_redis 和 redis-cli 的区别(主要是返回值类型和参数用法) 目录: Connection(连接) AUTH ECHO PING ...
- Redis在python中的使用
一 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...
- 【*】Redis实战场景中相关问题
一.Redis简介 redis主要解决的问题 分布式缓存是分布式系统中的重要组件,主要解决高并发.大数据场景下,热点数据访问的性能问题,提供高性能的数据快速访问. 使用缓存常见场景 项目中部分数据访问 ...
- redis info命令中各个参数的含义
Redis 性能调优相关笔记 2016年09月25日 15:42:04 WenCoding 阅读数:4844更多 个人分类: Redis数据库 info可以使用info [类别]输出指定类别内容i ...
- Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码
Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...
- 帆软报表FineReport中数据连接的JDBC连接池属性问题
连接池原理 在帆软报表FineReport中,连接池主要由三部分组成:连接池的建立.连接池中连接使用的治理.连接池的关闭.下面就着重讨论这三部分及连接池的配置问题. 1. 连接池原理 连接池技术的核心 ...
随机推荐
- Pytest执行用例报Hint: make sure your test modules/packages have valid Python names.
近日,使用Pytest+Appium 实现APP端UI自动化,遇到Pytest收集用例失败的情况. 报错信息如下: test_room.py:None (test_room.py) ImportErr ...
- K8S从入门到放弃系列-(12)Kubernetes集群Coredns部署
摘要: 集群其他组件全部完成后我们应当部署集群 DNS 使 service 等能够正常解析,1.11版本coredns已经取代kube-dns成为集群默认dns. 1)下载yaml配置清单 [root ...
- Hadoop学习(6)-HBASE的安装和命令行操作和java操作
使用HABSE之前,要先安装一个zookeeper 我以前写的有https://www.cnblogs.com/wpbing/p/11309761.html 先简单介绍一下HBASE HBASE是一个 ...
- TypeScript 高级类型
⒈交叉类型(Intersection Types) 交叉类型是将多个类型合并为一个类型. 这让我们可以把现有的多种类型叠加到一起成为一种类型,它包含了所需的所有类型的特性. 例如, Person &a ...
- java多线程上篇(一)
操作系统与程序运行以及进程简介 一.线程与操作系统 操作系统是对计算机硬件资源的管理程序,是应用程序与计算机硬件交互的中间层,其本质仍旧是运行于硬件电路上的程序 对计算机硬件来说不存在操作系统,只是处 ...
- 单例模式中volatile关键字的作用
背景&问题 在早期的JVM中,synchronized存在巨大的性能开销.因此,有人想出了一个"聪明"的技巧:双重检查锁定(Double-Checked Locking). ...
- ActiveMQ Queue示例
一.Queue 发送 public class JmsSend { public static void main(String[] args) throws JMSException { Conne ...
- sysbench安装和测试
1.首先安装依赖 yum install mariadb-devel yum install automake libtool –y 2.下载安装包 wget https://github.com/a ...
- Linux删除Tomcat中产生的所有log文件
#!/bin/bash #!/bin/bash #exact all log files #计算log文件个数log_number=`ls *.log |grep log -c` #当log文件数大于 ...
- RMAN执行crosscheck archive报错ORA-19633问题处理
一.问题现象 RMAN connect target /; run { crosscheck archivelog all; } ORA-: control file record is out of ...