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 eviction="LRU" type="cn.jbit.cache.RedisCache"/>
由于是第一次使用redis,再调试代码得时候报错:Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool] with root cause
无法获得redis的链接。
方法1.重新配置了redis连接池得参数:需要按照自己缓存的数量而设置最大链接数。
#最大空闲数,数据库连接的最大空闲时间。超过空闲数量,数据库连接将被标记为不可用,然后被释放。设为0表示无限制
redis.maxIdle=50
#最大连接数:能够同时建立的“最大链接个数”#jedis的最大活跃连接数设为0表示无限制,这个属性就是高版本的maxTotal redis.maxActive=50
#最大等待时间:单位ms
#jedis池没有连接对象返回时,等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。
#如果超过等待时间,则直接抛出JedisConnectionException
redis.maxWait=1000
##############################问题注解###############################
注解:运行报错:Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException:
Could not get a resource from the pool] with root cause maxActive是最大激活连接数,这里取值为50,表示同时最多有50个数据连 接。maxIdle是最大的空闲连接数,这里取值为50,
表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,随时处于待命状态。MaxWait是最大等待秒钟数,这里取值-1,
表示无限等待,直到超时为止,一般取值3000,表示3秒后超时。
而自己开始的设置是:redis.maxIdle=10 redis.maxActive=50
#########################################################################
如果问题继续存在
方法2.问题还是没解决,多次调试,发现链接资源没释放有关系,当然我的代码中是做了资源释放的。
先看poolConfig
<!-- redis数据源 -->
<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>
redis.properties文件配置
#最大链接数
redis.maxTotal=100
#最大空闲数,数据库连接的最大空闲时间。超过空闲数量,数据库连接将被标记为不可用,然后被释放。设为0表示无限制
redis.maxIdle=20
##jedis的最大活跃连接数设为0表示无限制
redis.maxActive=100
#最大等待时间:单位ms
#jedis池没有连接对象返回时,等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。
#如果超过等待时间,则直接抛出JedisConnectionException
redis.maxWait=1000 #使用连接时,检测连接是否成功 redis.testOnBorrow=true
redis文件
public void clear() {
JedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
connection.flushDb();
connection.flushAll();
System.out.println("clear=redis======>");
} catch (JedisConnectionException e) {
connection.close();//释放链接
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();//释放连接
}
}
}
问题的主要原因是使用连接池的链接后没有释放资源,当然开始我的代码就释放了使用的链接资源,但是还是会出现链接资源拿不到的情况。
可能是因为异常没有释放链接资源,我这个getConnection()是使用的第三方静态注入依赖于ehcache,只需要在需要缓存的dao或者mapper
加入注解,即可缓存并同步刷新最新数据。不需要在业务层手动的set,update,remove数据。
如果是getResource()这种方式获取的redis链接,用returnToPool(jedis)或jedis.close()是可以解决问题的;
最让我们忽略的原因就是你的redis是山寨版集成的,从新手博客上直接copy被坑的不要不要的。要么用spring boot集成的redis要么集成原生的。
具体情况酌情处理
redis使用问题一:Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisException: Could not get a resource from the pool] with root cause的更多相关文章
- redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
超时 Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionException: jav ...
- ERR Unsupported CONFIG parameter: notify-keyspace-events; nested exception is redis.clients.jedis.exceptions.JedisDataException
异常信息 时间:2017-04-05 15:53:57,361 - 级别:[ WARN] - 消息: [other] The web application [ROOT] appears to hav ...
- redis报错:java.net.SocketException: Broken pipe (Write failed); nested exception is redis.clients.jedis.exceptions.JedisConnectionException: java.net.SocketException: Broken pipe (Write failed)
最近写了一个服务通过springboot构建,里面使用了redis作为缓存,发布到服务器运行成功,但是有时候会报redis的错误:org.springframework.data.redis.Redi ...
- 记一次jedis并发使用问题JedisException: Could not return the resource to the pool
今天线上突然发现个奇怪的问题项目第一次启动的时候redis报错JedisException: Could not return the resource to the pool 直接访问接口的时候不报 ...
- Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException:
七月 17, 2014 4:56:01 下午 org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service( ...
- jdbc连接oracle时报错 Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableC
错误: Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; ...
- Spring 整合Mybatis 出现了Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create Poola
我出现的 报错信息如下: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionExc ...
- 2016.11.10 Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver
运行项目rds_web时,出现错误提示:Could not get JDBC Connection; nested exception is java.sql.SQLException: No sui ...
- Could not get JDBC Connection; nested exception is java.sql.SQLException: ${jdbc.driver}
在一个SSM分布式项目中一个服务报错: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnec ...
随机推荐
- Codeforces191 C. Fools and Roads
传送门:>Here< 题意:给出一颗树,和K次操作.每次操作给出a,b,代表从a到b的路径上所有边的权值都+1(边权最开始全部为0).最后依次输出每条边最终的权值 解题思路: 由于n非常大 ...
- python的小练习
# -*- coding: utf-8 -*- """练习:有1,2,3,4. 4个数能组成多少个互不相同且无重复数字的三位数,分别是多少?""&qu ...
- 【XSY2730】Ball 多项式exp 多项式ln 多项式开根 常系数线性递推 DP
题目大意 一行有\(n\)个球,现在将这些球分成\(k\) 组,每组可以有一个球或相邻两个球.一个球只能在至多一个组中(可以不在任何组中).求对于\(1\leq k\leq m\)的所有\(k\)分别 ...
- 解决nginx发布网站跨目录访问
解决nginx发布网站跨目录访问(thinkphp5+lnmp) 到:usr/local/nginx/conf/vim fastcgi.cof 把最后一行加上井号#注释掉保存重启 restart 参考 ...
- $NOIp2018$劝退记
鸽子博主好久没更博了,这一更可能以后都更不了了啊 \(Day~~1\) 考试爆零,已经无所畏惧了. 当作攒rp吧...qwq 晚上写了写数学总结,蒯了一堆人的博客资料,然后就学会了\(CRT\),\( ...
- Hdoj 2899.Strange fuction 题解
Problem Description Now, here is a fuction: F(x) = 6 * x^7+8x^6+7x^3+5x^2-yx (0 <= x <=100) Ca ...
- NOIP2011Mayan游戏(模拟)
Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个77 行\times 5×5列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指 ...
- 牛客练习赛43 Tachibana Kanade Loves Probability(快速幂)
链接:https://ac.nowcoder.com/acm/contest/548/B来源:牛客网 题目描述 立华奏在学习初中数学的时候遇到了这样一道大水题: “设箱子内有 n 个球,其中给 m 个 ...
- Manthan, Codefest 18 (rated, Div. 1 + Div. 2) C D
C - Equalize #include<bits/stdc++.h> using namespace std; using namespace std; string a,b; int ...
- spring5 reactive
示例代码:https://github.com/srpraneeth/spring5-reactive 说明文档: https://coyee.com/article/12086-spring-5-r ...