本文使用的是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的更多相关文章

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

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

  2. 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 ...

  3. 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 ...

  4. 记一次jedis并发使用问题JedisException: Could not return the resource to the pool

    今天线上突然发现个奇怪的问题项目第一次启动的时候redis报错JedisException: Could not return the resource to the pool 直接访问接口的时候不报 ...

  5. 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( ...

  6. 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; ...

  7. 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 ...

  8. 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 ...

  9. Could not get JDBC Connection; nested exception is java.sql.SQLException: ${jdbc.driver}

    在一个SSM分布式项目中一个服务报错: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnec ...

随机推荐

  1. idea maven +spring mvc

    1.步骤一 2.目录结构 3.maven <!--测试--> <dependency> <groupId>junit</groupId> <art ...

  2. 用递归方法计算斐波那契数列(Recursion Fibonacci Sequence Python)

    先科普一下什么叫斐波那契数列,以下内容摘自百度百科: 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因意大利数学家列昂纳多·斐波那契(Leonardoda Fibonacci ...

  3. 第二届强网杯wp

    web web签到 利用了md5碰撞 payload为 param1 =%4d%c9%68%ff%0e%e3%5c%20%95%72%d4%77%7b%72%15%87%d3%6f%a7%b2%1b% ...

  4. javascript之复习(css属性值的计算)

    js取div的宽高咋办,css有content-box,border-box,padding-box,表现又不一样.好在有个offsetWidth, <style type="text ...

  5. 关于json_encode转数组为json对象时里有数组格式数据的问题

    前言:这次是给一款小程序提供接口时发现的 别的不多说,下面直接看出现问题的json数据 可以看到,这是一个大的json对象,是由多维数组组成,一般api接口提供的也是这种格式. 但是仔细看红框中的内容 ...

  6. 使用海康的某款摄像头以及v4l2的经验

    Video Capture模式下发现使用MJPEG流和YUYV流相同分辨率视野却不同. 最后没找到怎么设物理窗口或图片窗口的方法,做实验知道了为什么. 设为1280×720 YUYV实际物理窗口是19 ...

  7. java web整合office web apps

    1.下载安装vmware虚拟机 2.下载windows server 2012或者window server 2012 R2的iso镜像 http://www.xp85.com/html/Window ...

  8. 一个小误区 JS中的contains

    在Java语言中,contains可以用于判断str1是否包含str2 原生JS中是有contains方法的 但它并不是字符串方法,,仅用于判断DOM元素的包含关系,参数是Element类型 若要在J ...

  9. IntelliJ IDEA载入JDBC驱动包

    1.下载zip格式的驱动包:https://dev.mysql.com/downloads/connector/j/ 2.解压zip,放到任意位置.其中的mysql-connector-java.ja ...

  10. linux 进程创建clone、fork与vfork

    目录: 1.clone.fork与vfork介绍 2.fork说明 3.vfork说明 4.clone说明5.fork,vfork,clone的区别 内容: 1.clone.fork与vfork介绍 ...