一、背景

  项目中使用spring框架整合redis,使用框架封装的RedisTemplate来实现数据的增删改查,项目上线后,我发现运行一段时间后,会出现异常Could not get a resource from the pool。起初我是觉得redis的最大连接数不够,所以一味地增大最大连接数,试了几次,发现还是报异常:Could not get a resource from the pool。但是看到连接池中明显有链接,况且此问题一般是资源池的连接数大于设置的最大连接数才出现。最后怀疑是没有释放连接。so网上搜,解决了这个问题。

Caused by: redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:51)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:226)
at redis.clients.jedis.JedisPool.getResource(JedisPool.java:16)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:191)
... 86 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:449)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:363)
at redis.clients.util.Pool.getResource(Pool.java:49)
... 89 more

二、解决

问题的关键在于redis的配置文件中enableTransactionSupport的设置。

1、enableTransactionSupport = false 。此时redis不支持事物。RedisTemplate会在使用完了之后自动释放连接;

2、enableTransactionSupport = true 。此时redis支持事物。RedisTemplate是不会主动释放连接的,需要手动释放连接,此问题可以参考这篇文档:Sping Data Redis 使用事务时,不关闭连接的问题

redisTemplate.exec();
RedisConnectionUtils.unbindConnection(redisTemplate.getConnectionFactory());

三、附录

附上阿里云redis的常见异常文档

1、Jedis常见异常汇总

2、JedisPool资源池优化

												

spring整合redis使用RedisTemplate的坑Could not get a resource from the pool的更多相关文章

  1. 网站性能优化小结和spring整合redis

    现在越来越多的地方需要非关系型数据库了,最近网站优化,当然从页面到服务器做了相应的优化后,通过在线网站测试工具与之前没优化对比,发现有显著提升. 服务器优化目前主要优化tomcat,在tomcat目录 ...

  2. Spring整合Redis&JSON序列化&Spring/Web项目部署相关

    几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...

  3. spring整合redis之hello

    1.pom.xml文件 <dependencies> <!-- spring核心包 --> <dependency> <groupId>org.spri ...

  4. Spring整合Redis时报错:java.util.NoSuchElementException: Unable to validate object

    我在Spring整合Redis时报错,我是犯了一个很低级的错误! 我设置了Redis的访问密码,在Spring的配置文件却没有配置密码这一项,配置上密码后,终于不报错了!

  5. Redis的安装以及spring整合Redis时出现Could not get a resource from the pool

    Redis的下载与安装 在Linux上使用wget http://download.redis.io/releases/redis-5.0.0.tar.gz下载源码到指定位置 解压:tar -xvf ...

  6. Spring整合redis实现key过期事件监听

    打开redis服务的配置文件   添加notify-keyspace-events Ex  如果是注释了,就取消注释 这个是在以下基础上进行添加的 Spring整合redis:https://www. ...

  7. JAVA整合Redis使用redisTemplate清除库中的所有键值对数据

    JAVA整合Redis使用redisTemplate清除库中的所有键值对数据,清除所有缓存数据 Set<String> keys = redisTemplate.keys("*& ...

  8. redis使用问题一:Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool] with root cause

    本文使用的是spring-data-redis 首先说下redis最简单得使用,除去配置. 需要在你要使用得缓存得地方,例如mybatis在mapper.xml中加入: <cache evict ...

  9. redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

    超时 Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: jav ...

随机推荐

  1. 从零开始搭建运维体系 - ansible

    从零开始搭建运维体系 - ansible 基本配置好了局域网内的机器后,第一个遇到的问题就是如何批量操作这么多台机器,ansible就是这么一个自动化运维工具. ansible是一个基于ssh的批量远 ...

  2. Python:黑板课爬虫闯关第三关

    第三关开始才算是进入正题了. 输入网址 http://www.heibanke.com/lesson/crawler_ex02/,直接跳转到了 http://www.heibanke.com/acco ...

  3. 微信小程序开发01-小程序的执行流程是怎么样的?

    前言 我们这边最近一直在做基础服务,这一切都是为了完善技术体系,这里对于前端来说便是我们需要做一个Hybrid体系,如果做App,React Native也是不错的选择,但是一定要有完善的分层: ① ...

  4. 《k8s-1.13版本源码分析》上github

    要干嘛? 猪年新气象,今年开始,kubernetes源码分析系列文章主战场从微信公众号转至github,完全使用Markdown重写,使用gitbook生成web页面,支持在线阅读,导出pdf等各种玩 ...

  5. Flutter 即学即用系列博客——08 MethodChannel 实现 Flutter 与原生通信

    背景 前面我们讲了很多 Flutter 相关的知识点,但是我们并没有介绍怎样实现 Flutter 与原生的通信. 比如我在 Flutter UI 上面点击了一个按钮,我希望原生做一些处理,那么原生怎么 ...

  6. 数据库艰难求生之路(基础:创建数据库表格)part1

     创建表格 1.创建表格之identity create table TableName( id ,), col_1 ), col_2 ) ) 这是一个简单的表格创建,identity这个是指在创建表 ...

  7. mysql命令行导入导出数据库

    导出:1.在命令行里,进入mysql安装根目录下的bin目录下比如:D:\Program Files\MySQL\MySQL Server 5.0\bin输入 mysqldump -uroot -p ...

  8. Django之随机图形验证码

    实现效果:点击右边图片验证码会变 前端代码: <div class="container"> <div class="row"> < ...

  9. Docker的使用

    Ubuntu16.04+ 在Ubuntu系统中安装较为简单,官方提供了脚本供我们进行安装. sudo apt install curl curl -fsSL get.docker.com -o get ...

  10. Python网络爬虫-信息标记

    信息标记的三种形式: XML(扩展标记语言) JSON(js中面向对象的信息表达形式,由类型的(string)键值对组成) "name":"北京理工大学" YA ...