Jedis Object has already been returned to this pool or is invalid
来源:互联网 发布:日本对外贸易数据2015 编辑:程序博客网 时间:2024/06/03 22:24
在工作中使用了Redis,同时在公司自己的系统中封装了Redis的JAVA端jedis。为了提高Jedis的效率,提高系统资源使用率,又使用了Jedis Pool。 为了方便使用,在底层封装了JedisPool。但在进行初步测试时,出现了如下问题:
Exception in thread "pool-3-thread-19" redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool
at redis.clients.jedis.JedisPool.returnResource(JedisPool.java:256)
at org.steed.core.redis.util.RedisInstance.recycleJedis(RedisInstance.java:57)
at org.steed.bussiness.stock.redis.cache.StockChangedProductQueue$1.run(StockChangedProductQueue.java:34)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool
at redis.clients.util.Pool.returnResourceObject(Pool.java:69)
at redis.clients.jedis.JedisPool.returnResource(JedisPool.java:253)
... 5 more
Caused by: java.lang.IllegalStateException: Object has already been returned to this pool or is invalid
at org.apache.commons.pool2.impl.GenericObjectPool.returnObject(GenericObjectPool.java:551)
at redis.clients.util.Pool.returnResourceObject(Pool.java:67)
... 6 more
at redis.clients.jedis.JedisPool.returnResource(JedisPool.java:256)
at org.steed.core.redis.util.RedisInstance.recycleJedis(RedisInstance.java:57)
at org.steed.bussiness.stock.redis.cache.StockChangedProductQueue$1.run(StockChangedProductQueue.java:34)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: redis.clients.jedis.exceptions.JedisException: Could not return the resource to the pool
at redis.clients.util.Pool.returnResourceObject(Pool.java:69)
at redis.clients.jedis.JedisPool.returnResource(JedisPool.java:253)
... 5 more
Caused by: java.lang.IllegalStateException: Object has already been returned to this pool or is invalid
at org.apache.commons.pool2.impl.GenericObjectPool.returnObject(GenericObjectPool.java:551)
at redis.clients.util.Pool.returnResourceObject(Pool.java:67)
... 6 more
以上问题就是说 Jedis对象被提前关闭了。使用JedisPool时一般获取jedis与关闭Jedis是同步出现的,一旦Jedis提前关闭或者Jedis对象不一致,都可能出现上述问题。
经过分析代码,发现代码使用了线程池,同时使用ThreadLocal保存了Jedis对象。第一次取值时,因为当前线程变量不存在,则从ThreadLocal中取出的值为空,此时从Jedis线程池中取出一个对象,在使用完毕后关闭了jedis;第二次取值时,因为当前线程变量中已经存在了Jedis(为什么会存在呢?且看下文),则从ThreadLocal中取出的值不为空,而此时jedis因为已经close了,所以会出现上述错误。
以上问题可以向我们说明了一点,当我们使用了线程池同时也使用了ThreadLocal时,应该在每次线程结束时清空ThreadLocal中保存的数据,只用这样才不会对以后的操作产生干扰。
转载时请注明出处:http://blog.csdn.net/yumingbeizhan/article/details/76889969
阅读全文
2 0
- Jedis Object has already been returned to this pool or is invalid
- 使用apache commons pool时遇到IllegalStateException:returned object is not part of this pool
- java.sql.SQLException: Connection has already been created in this tx context for pool named ...
- mysql出现Connection.close() has already been called. Invalid operation in this state.
- getOutputStream() has already been called for this
- VideoError: 1008: VideoPlayer.iNCManagerClass has not been set or its value is invalid
- Yarn application has already ended! It might have been killed or unable to launch application maste
- Tomcat异常:Illegal access: this web application instance has been stopped already. Could not load java.lang.Object.
- java_lang_IllegalStateException getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response 的解决方法
- Illegal access: this web application instance has been stopped already
- getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response的解决方法
- getOutputStream() has already been called for this response 异常处理
- getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response
- getOutputStream() has already been called for this response 的解决方法
- php中奖概率算法,可用于刮刮卡,大转盘等抽奖算法
- Sheet列号与数字之间的相互转换
- JMeterPlugin 插件安装后,不能跑起来异常 解析(jmeter 3.2)
- hbase中Bytes类的使用
- 解决JDK的安装路径中有空格的情况
- Jedis Object has already been returned to this pool or is invalid
- Caffe(CPU)-Ubuntu16.04下安装教程(未完成)
- php mysql数据库备份
- leetcode--Construct Binary Tree from Preorder and Inorder Traversal
- 蓝桥杯-兰顿蚂蚁(Java)
- poj 2777 Count Color(线段树区间更新+技巧)
- lintcode刷题——快速幂
- javascript/js 原生动态引入外部css文件及动态插入css代码片段
- FileZilla上传文件到外网FTP服务器响应:550 Permission denied.