redis使用连接池报错解决
redis使用十几小时就一直报异常

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at redis.clients.util.Pool.getResource(Pool.java:22)
at com.derbysoft.jredis.longkeytest.BorrowObject.run(BorrowObject.java:22)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1134)
at redis.clients.util.Pool.getResource(Pool.java:20)

原因是没有回收资源导致
正确的做法是

ShardedJedis jedis = shardedJedisPool.getResource();
try {
jedis.set(key, value);
} finally {
shardedJedisPool.returnResource(jedis);
}

更新内容:

如果你用的jedis 2.4.2以及以前版本,用完之后别忘了return连接到资源池。

不过2.5.0版本之后,jedis使用了try-with-resource,jedis用完了就会自动归还了,不用每次都自己return了。

各种链接数的参数自己调一下。例如MaxTotal,MaxIdle等等~

如果redis server不在局域网内,可能会因为network问题导致链接超时,会抛出socket的异常,当然写数据就也会丢失。

connection的timeout默认是2000ms,你可以自己调的大一些,这样网络慢的时候就不至于丢失数据。

十分重要的一点:在开发的时候,方法A调用方法B,方法A需要获取jedis 链接,方法B也要获取jedis链接,在实现的时候尽量让他们公用连接,例如B用A的链接,这样效率会快非常多,而且也不会浪费链接。

最重要的一点,从google老外回答:

1.I test it using a multi-thread code, when the poolSize > maxclients(redis.conf),
it will throw this exception. And it runs well when the maxclients is larger than poolSize.

2.If you set maxclients to 10, and set fixed thread pool size to 20, it prints ".. fail to get connection...".
If you set maxclients to 10, and set fixed thread pool size to 5, it runs well.

从而得之,在redis配置文件,redis.conf文件中,有个参数设置,maxclients(默认是注释掉的,不设置此值时表示无redis客户端连接个数限制)。

设置时,maxclients的值要大于poolSize(最大连接池的链接个数)。

redis运用连接池报错解决的更多相关文章

  1. Centos7使用python3连接inception报错解决办法

    inception支持mysqldb库但不支持pymysql库,无奈mysqldb库不兼容py3,直接使用pymysql 连接inception报错如下: ValueError: invalid li ...

  2. 连接池报错 Proxool Provider unable to load JAXP configurator file: proxool.xml

    上篇博文讲到简易配置 proxool 连接池:http://www.cnblogs.com/linnuo/p/7232380.html 由于把说明注释留在了 proxool.xml 配置文件里导致配置 ...

  3. 【转】java.sql.SQLException: statement is closed语句被关闭 druid连接池报错

    我之前在用druid 1.0.28版本也出现过这个问题, 现象就是: 报这个错的时候, 往往会出现在一条毫无错误的sql执行上报错,  sql放到数据库上执行或者单独拎出来执行完全没问题, 但是为什么 ...

  4. 详解Jedis连接池报错处理

    在使用Jedis连接池模式下,比较常见的报错如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a re ...

  5. Druid连接池 报错:abandon connection原因分析

    问题现象:使用Druid的数据库连接池,在进行一个查询SQL的时候,抛出了异常: [2017-10-20 01:40:59.269 ERROR com.alibaba.druid.pool.Druid ...

  6. ssh 或 putty 连接linux报错解决方法

    由于当天多次输入错误密码,ssh和putty就连接不上了,纠结了很久解决问题 ssh连接提示错误:server unexpectedly closed network connection putty ...

  7. springjdbc使用c3p0连接池报错 java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector

    MyMaincom.test.sunc.MyMaintestMethod(com.test.sunc.MyMain)org.springframework.beans.factory.BeanCrea ...

  8. python连接mariadb报错解决1045, "Access denied for user 'root'@'192.168.0.50' (using password: YES)

    [root@localhost ~]# python Python 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC 4.8.5 20150623 (Red Ha ...

  9. jekins自动部署tomcat注意事项、连接tomcat报错

    jekins自动部署tomcat注意事项 千万不要用下面插件推送,报错很多, 要用脚本,一篇博客说的:“我们都是用的脚本,插件报错太多,也不完善” Deploy to container Plugin ...

随机推荐

  1. linux里的vi怎么移动到最后一行

    下面是几个vi与行移动有关的命令: G:光标移至最后一行 nG:光标移至第n行首 n+:光标下移n行 n-:光标上移n行 注意输入命令,需要首先按ESC键回到命令模式. 转自: http://zhid ...

  2. 《ASP.NET1200例》各种类型文件汇总

    aspx是页面文件 ascx是用户控件,用户控件必须嵌入到aspx中才能使用. ascx是用户控件,相当于模板 其实ascx你可以理解为Html里的一部分代码, 只是嵌到aspx里而已, 因为aspx ...

  3. ExecutorService 和 NSOperationQueue

    ExecutorService,简化了Android中的并发处理,NSOperationQueue简化了iOS中的并发处理.它们都管理线程池,作用十分相近,下面简单说明一下. 1.ExecutorSe ...

  4. (原创)Python字符串系列(1)——str对象

    在本博客 <Python字符串系列> 中,将介绍以下内容: Python内置的str对象及操作 字符串的格式化 Python中的正则表达式 re模块 本文将介绍Python内置的 str ...

  5. IOS多线程(NSOperation,NSOperationQueue)

    含义:NSOperation,NSOperationQueue是什么. The NSOperation class is an abstract class you use to encapsulat ...

  6. byte[]和InputStream的相互转换[转载]

    1:byte[]转换为InputStream InputStream sbs = new ByteArrayInputStream(byte[] buf); 2:InputStream转换为Input ...

  7. Ubuntu12.04安装ia32-libs

    sudo apt-get install libc6:i386 sudo -i cd /etc/apt/sources.list.d// care for old-releases.ubuntu.co ...

  8. rsync --exclude 参数

    /usr/bin/rsync -vr --exclude=".svn" --exclude="temp" --delete /alidata/www/pro/e ...

  9. 2.简单工厂模式(Simple Factory)

    using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //如果 ...

  10. 1、揭秘通用平台的 HttpClient (译)

    原文链接:Demystifying HttpClient APIs in the Universal Windows Platform 正打算翻译这篇文章时,发现园子里已经有朋友翻译过了,既然已经开始 ...