今天测试,发现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. SQLite之rowid与sqlite3_last_insert_rowid()

    //返回最后一次insert的rowid,如果没有插入就返回0 (DB session断开后也返回0, 是保存在进程的内存中) SELECT LAST_INSERT_ROWID(); //找到最大的r ...

  2. 什么是SSH 以及常见的ssh 功能

    什么是SSH? 简单说,SSH是一种网络协议,用于计算机之间的加密登录.如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会泄露. ...

  3. JVM -- 虚拟机中的对象

    一.HotSpot虚拟机 它是Sun JDK和OpenJDK中所带的虚拟机,也是目前使用范围最广的Java虚拟机.我们大致知道虚拟机内存的概况,也许更想了解这些虚拟机内存的数据的其他细节,誓如它们是如 ...

  4. 如何理解Python中的None

    Python中的None是一个经常被用到的知识点,但是很多人对于None的内涵把握的还是不够精确,今天就和我一起好好理解下这个小知识点吧. 1.None表示空,但它不等于空字符串.空列表,也不等同于F ...

  5. WUSTOJ 1283: Hamster(Java)

    1283: Hamster 参考博客 wust_tanyao的博客 题目   第0个月有1对仓鼠.仓鼠的寿命是M个月,仓鼠成年后每个月生一对仓鼠(一雌一雄),问N个月后有仓鼠多少对.更多内容点此链接 ...

  6. spring cloud微服务实践一

    最近在学习spring框架.其中spring cloud在微服务方面很火,所以在学习过程中,也做一些记录. 注:这一个系列的开发环境版本为 java1.8, spring boot2.x, sprin ...

  7. java异常那些事

    异常的基本定义: 异常情形是指阻止当前方法或者作用域继续执行的问题.在这里一定要明确一点:异常代码某种程度的错误,尽管Java有异常处理机制,但是我们不能以“正常”的眼光来看待异常,异常处理机制的原因 ...

  8. python03-break、continue、for循环、数据bytes类型、字符串与字节的关系、变量指向与深浅拷贝、set集合、文件操作

    目录: 1.break.continue 2.for循环 3.数据bytes类型 4.字符串与字节的关系 5.变量指向与深浅拷贝 6.set集合 7.文件操作 一.break.continue bre ...

  9. (七)Hibernate中使用JDBC

    在hibernate中获取connection数据库连接有两种方法:(操作数据库常用这种方法) 1. session.doReturningWork   返回一个对象,适用于查询方法 2. sessi ...

  10. springboot启动流程(五)创建ApplicationContext

    所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 springboot在启动过程中将会根据当前应用的类型创建对应的ApplicationC ...