今天测试,发现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. 将 MathType 公式转换为 Word 自带公式

    以下操作是基于Office 365以及MathType 6.9b平台.有网友留言说第四步没出现「转换为 Office Math」选项,这个我就不清楚了,难道是只有Office 365才支持? 打开Ma ...

  2. 关闭iTunes自动同步

    方法步骤: 仅对iTunes安装在默认路径的生效. 在「运行」里边先后输入以下两条命令: "C:\Program Files\Common Files\Apple\Apple Applica ...

  3. 20190705-Python数据驱动之DDT

    DDT ddt 是第三方模块,需安装, pip install ddt DDT包含的装饰器 包含一个类装饰器@ddt和两个方法装饰器@data和@file_data 通常情况下,@data中的数据按照 ...

  4. dubbo中使用的zookeeper,netty以及javassist的Demo文件

    javassist测试Demo netty测试Demo zookeeper测试Demo

  5. Spring (3)框架

    Spring第三天笔记 今日内容 Spring的核心之一 -  AOP思想 (1) 代理模式- 动态代理 ① JDK的动态代理 (Java官方) ② CGLIB 第三方代理 (2) AOP思想在Spr ...

  6. 使用DANT做FTP的转发代理

    FTP不能直接使用nginx进行转发,想了一些办法,最后决定使用iptalbes做DNAT,相关于把这个机器做一台防火墙,做一个NAT 1.启用ip_forward vim /etc/sysctl.c ...

  7. C# wsdl.exe 生成类文件

    wsdl.exe D:\XXX\demand\demand.\wsdl\XXX.wsdl /\wsdl\class 在 vs tools:Developer Command Prompt For VS ...

  8. (一)SpringMvc简介以及第一个springmvc工程

    一.SpringMVC是什么? springmvc是Spring的一个模块,提供web层解决方案(就与MVC设计架构) 如上图, DispatcherServlet:前端控制器,由SpringMVC提 ...

  9. Node在Sublime Text3下环境搭建(node02)

    一.下载sublime Text的nodejs插件 https://github.com/tanepiper/SublimeText-Nodejs 二.下载后解压 直接改名为nodejs 放到 Pre ...

  10. Abp 领域事件简单实践 <一>

    领域事件,是领域内发生的事件引发别的操作,其他的类可以订阅这是事件. 接着上一篇,在testOrder 上实现一个接口  IEventHandler<EntityCreatingEventDat ...