redis运用连接池报错解决
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运用连接池报错解决的更多相关文章
- Centos7使用python3连接inception报错解决办法
inception支持mysqldb库但不支持pymysql库,无奈mysqldb库不兼容py3,直接使用pymysql 连接inception报错如下: ValueError: invalid li ...
- 连接池报错 Proxool Provider unable to load JAXP configurator file: proxool.xml
上篇博文讲到简易配置 proxool 连接池:http://www.cnblogs.com/linnuo/p/7232380.html 由于把说明注释留在了 proxool.xml 配置文件里导致配置 ...
- 【转】java.sql.SQLException: statement is closed语句被关闭 druid连接池报错
我之前在用druid 1.0.28版本也出现过这个问题, 现象就是: 报这个错的时候, 往往会出现在一条毫无错误的sql执行上报错, sql放到数据库上执行或者单独拎出来执行完全没问题, 但是为什么 ...
- 详解Jedis连接池报错处理
在使用Jedis连接池模式下,比较常见的报错如下: redis.clients.jedis.exceptions.JedisConnectionException:Could not get a re ...
- Druid连接池 报错:abandon connection原因分析
问题现象:使用Druid的数据库连接池,在进行一个查询SQL的时候,抛出了异常: [2017-10-20 01:40:59.269 ERROR com.alibaba.druid.pool.Druid ...
- ssh 或 putty 连接linux报错解决方法
由于当天多次输入错误密码,ssh和putty就连接不上了,纠结了很久解决问题 ssh连接提示错误:server unexpectedly closed network connection putty ...
- springjdbc使用c3p0连接池报错 java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
MyMaincom.test.sunc.MyMaintestMethod(com.test.sunc.MyMain)org.springframework.beans.factory.BeanCrea ...
- 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 ...
- jekins自动部署tomcat注意事项、连接tomcat报错
jekins自动部署tomcat注意事项 千万不要用下面插件推送,报错很多, 要用脚本,一篇博客说的:“我们都是用的脚本,插件报错太多,也不完善” Deploy to container Plugin ...
随机推荐
- 10 Python Optimization Tips and Issues
转自: http://www.algorithm.co.il/blogs/computer-science/10-python-optimization-tips-and-issues/
- 【JavaScript】SVG vs Canvas vs WebGL
参考资料: http://blog.csdn.net/lufy_legend/article/details/38292125 http://zhidao.baidu.com/link?url=e4n ...
- tcp粘包问题(封包)
tcp粘包分析 http://blog.csdn.net/zhangxinrun/article/details/6721495 解决TCP网络传输“粘包”问题(经典) http: ...
- Windows命令行重命名文件
RENAME D:\Cache\xyz.html xyz%date:~0,4%%date:~5,2%%date:~8,2%.tar.gz
- selec2 clone不起作用。
<table class="table table-bordered"> <thead> <tr> <th width ="16 ...
- iOS 中关于ViewController总结
以前写程序时,经常被旋转问题弄的头疼,今天为了解决这个问题,偶然看到了苹果官方文档 View Controller Programming Guide for iOS. 这才发现这个必读的资料!以前许 ...
- 利用 FFmpeg palettegen paletteuse 生成接近全色的 gif 动画
下载FFmpeg-VideoToGif-v1.0.bat.7z FFmpeg 2.6 及以上版本有效 未使用palette时 使用palette后 @echo off set inFile=2015. ...
- AU版有锁机的福利,704越狱彻底解决+86问题,完美IM/FT,重启不掉APN设置
http://bbs.25pp.com/thread-172881-1-1.html 串号99的是au版 串号013的是sb版 44050 AU为 找到咱们SB版的文件,为44020 http:/ ...
- unsatisfied类型的异常
一般为libs包下arm64-v8a,armeabi,armeabi-v7a,mips,mips64,x86,x86_64等文件夹下的.so文件丢失. 一般情况,armeabi下的so文件需要拷贝一份 ...
- java单例,懒汉&饿汉
* 单例模式Singleton * 应用场合:有些对象只需要一个就足够了,如皇帝 * 作用: 保证整个应用程序中某个实例有且只有一个 * 区别: 饿汉模式的特点是加载类时比较慢,但运行是比较快 ...