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 ...
随机推荐
- Codeforces510 D. Fox And Jumping
Codeforces题号:#510D 出处: Codeforces 主要算法:map+DP 难度:4.6 思路分析: 题意:给出n张卡片,分别有l[i]和c[i].在一条无限长的纸带上,你可以选择花c ...
- 【XSY2701】异或图 线性基 容斥原理
题目描述 定义两个图\(G_1\)与\(G_2\)的异或图为一个图\(G\),其中图\(G\)的每条边在\(G_1\)与\(G_2\)中出现次数和为\(1\). 给你\(m\)个图,问你这\(m\)个 ...
- 【XSY1580】Y队列 容斥
题目大意 给你\(n,r\),求第\(n\)个不能被表示为\(a^b(2\leq b\leq r)\)的数 \(n\leq 2\times {10}^{18},r\leq 62\) 题解 我们考虑二分 ...
- bzoj 2429: [HAOI2006]聪明的猴子 (最小生成树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2429 思路:就是找最小生成树最大的一条边,最小生成树的性质,最后加入的那条边就是最大的 实现 ...
- MT【189】二次条件配方
“当一幢建筑物完成时,应该把脚手架拆除干净.”——高斯 (2017北大特优)若对任意使得关于 \(x\) 的方程 \(ax^2+bx+c=0\)(\(ac\ne 0\))有实数解的 \(a,b,c\) ...
- hdu5017 Ellipsoid (模拟退火)
Ellipsoid 原题链接 题目描述 给定.一个要满足的椭球的方程\(ax^2+by^2+cz^2+dyz+exz+fxy=1\) 求球面上一个点到原点\((0,0,0)\)的距离最小. 有多组输入 ...
- Spring学习记录
Java类定义配置@Configuration //标记为配置类@ComponentScan //标记为扫描当前包及子包所有标记为@Component的类@ComponentScan(basePack ...
- webRequest封装
from requests.models import Response import requests import random import time class WebRequest(obje ...
- hdu 1024 Max Sum Plus Plus(m段最大和)
Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...
- css 蒙层
蒙层 利用z-index: .mui-backdrop-other { position: fixed; top: 44px; right:; bottom:; left:; z-index:; ba ...