今天测试,发现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在应用中使用连接不释放问题解决的更多相关文章

  1. ConnectionPool实现redis在python中的连接

    这篇文章主要介绍了Python与Redis的连接教程,Redis是一个高性能的基于内存的数据库,需要的朋友可以参考下   今天在写zabbix storm job监控脚本的时候用到了python的re ...

  2. Redis安装 java中的连接 序列化 反序列化

    安装路径 /webapp/redis/redis- #启动redis /webapp/redis/redis-/src/redis-server & #关闭redis /webapp/redi ...

  3. Redis】Java中使用Jedis操作Redis(Maven导入包)、创建Redis连接池

    如果我们使用Java操作Redis, 需要确保已经安装了 redis 服务及 Java redis 驱动. Maven项目可以直接在pom.xml中加入jedis包驱动: <!-- https: ...

  4. redis 在 php 中的应用(Connection [ 连接] 篇)

    本文为我阅读了 redis参考手册 之后编写,注意 php_redis 和 redis-cli 的区别(主要是返回值类型和参数用法) 目录: Connection(连接) AUTH ECHO PING ...

  5. Redis在python中的使用

    一 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...

  6. 【*】Redis实战场景中相关问题

    一.Redis简介 redis主要解决的问题 分布式缓存是分布式系统中的重要组件,主要解决高并发.大数据场景下,热点数据访问的性能问题,提供高性能的数据快速访问. 使用缓存常见场景 项目中部分数据访问 ...

  7. redis info命令中各个参数的含义

    Redis 性能调优相关笔记 2016年09月25日 15:42:04 WenCoding 阅读数:4844更多 个人分类: Redis数据库   info可以使用info [类别]输出指定类别内容i ...

  8. Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码

    Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...

  9. 帆软报表FineReport中数据连接的JDBC连接池属性问题

    连接池原理 在帆软报表FineReport中,连接池主要由三部分组成:连接池的建立.连接池中连接使用的治理.连接池的关闭.下面就着重讨论这三部分及连接池的配置问题. 1. 连接池原理 连接池技术的核心 ...

随机推荐

  1. 获取Android手机日志

    方式一:使用USB连接 1.在手机上启用USB调试2.在终端输入adb devices 3.获取日志 只连接一个设备:1)清除已缓存日志:adb logcat -c2)获取日志并保存到本地:adb l ...

  2. TypeScript symbol类型

    自ECMAScript 2015(ES6)起,symbol成为了一种新的原生类型,就像基本类型number和string一样. ⒈介绍及使用方式 TypeScript中使用symbol类型和JavaS ...

  3. Win10 彻底关闭 Windows Defender

    1.使用快捷键 WIN+R 调出运行工具,然后再输入组策略命令 gpedit.msc 再点击确定. 2.进入组策略在计算机配置下面的管理模板,Windows 组件就可以看到 Windows Defen ...

  4. java poi3.10.1基本excel使用

    效果: 代码: //时间 String checkTime = "yyyy/MM/dd"; //人员查询 Set preParticipantNames = new HashSet ...

  5. Excel常用操作1

    1.数据透视 所在选项卡:插入-数据透视表 例如:查看下表中各个工龄的平均工资 数据透视:选择所有数据--数据透视表--数据透视字段:选择工作经验和salary 切片器的使用,根据工作经验进行切片(还 ...

  6. 网络知识(1)TCP/IP五层结构

    图1 数据流向图 1,网络基础 1.1 发展 古代:①烽火狼烟最为原始的0-1单bit信息传递:②飞鸽传书.驰道快马通信,多字节通信: 近代:①轮船信号灯:②无线电报[摩尔斯码]: 现代:①有线模拟通 ...

  7. SQL优化中的重要概念:死锁

    原文:SQL优化中的重要概念:死锁 上面几篇文章讲到 事务.锁定.阻塞,最后还有一种比较极端的情况,就是死锁,这也是锁定.阻塞的一种情况. 死锁是当两个事务分别锁定了资源,而又继续请求对方已获取的资源 ...

  8. vue 集成jTopo 处理方法

    jTopo 帮助说明网站 http://www.jtopo.com/index.html 使用例子: http://www.jtopo.com/demo/helloworld.html 不建议直接安装 ...

  9. Ctrl+R快速启动应用程序

    1.打开注册表 Ctrl+R搜索框中键入“regedit”,回车打开注册表 2.找到HKEY_LOCAL_MACHINE中的如下路径 HKEY_LOCAL_MACHINE\SOFTWARE\Micro ...

  10. opencv-04--图像金字塔

    图像金字塔被广泛应用于各种视觉应用中.图像金字塔是一个图像集合,集合中图像都源于同一个原始图像,而且是通过对原始图像连续降采样获得,直到达到某个中止条件才停止降采样.(当然,降为一个像素肯定是中止条件 ...