JedisDataException: Please close pipeline or multi block before calling this method
错误原因
transaction错误使用
pipeline错误使用
具体分析
transaction错误使用
- Redis事务的执行结果是在exec之后才统一返回,所以Jedis会用一个Response对象最为事务对象transaction的执行放回值。如果我们在transaction执行exec方法之前调用response对象的get方法会出现异常:
- Exception in thread "main" redis.clients.jedis.exceptions.JedisDataException: Please close pipeline or multi block before calling this method.
- 更多Redis事务操作,请移步:https://blog.csdn.net/fly910905/article/details/78981084
pipeline错误使用
1.异常堆栈
redis.clients.jedis.exceptions.JedisDataException: Please close pipeline or multi block before calling this method.
2.异常描述:
在pipeline.sync()执行之前,通过response.get()获取值,在pipeline.sync()执行前,命令没有执行(可以通过monitor做验证),下面代码就会引起上述异常
Jedis jedis = new Jedis("127.0.0.1", 6379);
Pipeline pipeline = jedis.pipelined();
pipeline.set("hello", "world");
pipeline.set("java", "jedis");Response<String> pipeString = pipeline.get("java");
//这个get必须在sync之后,如果是批量获取值建议直接用List<Object> objectList = pipeline.syncAndReturnAll();
System.out.println(pipeString.get());
//命令此时真正执行
pipeline.sync();
Jedis中Reponse中get()方法,有个判断:如果set=false就会报错,而response中的set初始化为false.
public T get() {// if response has dependency response and dependency is not built,// build it first and no more!!if (dependency != null && dependency.set && !dependency.built) {dependency.build();}if (!set) {throw new JedisDataException("Please close pipeline or multi block before calling this method.");}if (!built) {build();}if (exception != null) {throw exception;}return response;
}
pipeline.sync()会每个结果设置set=true。
public void sync() {if (getPipelinedResponseLength() > 0) {List<Object> unformatted = client.getAll();for (Object o : unformatted) {generateResponse(o);}}
}
其中generateResponse(o):
protected Response<?> generateResponse(Object data) {Response<?> response = pipelinedResponses.poll();if (response != null) {response.set(data);}return response;
}
其中response.set(data);
public void set(Object data) {this.data = data;set = true;
}
3.解决方法:
实际上对于批量结果的解析,建议使用pipeline.syncAndReturnAll()来实现,下面操作模拟了批量hgetAll
/**
* pipeline模拟批量hgetAll
* @param keyList
* @return
*/
public Map<String, Map<String, String>> mHgetAll(List<String> keyList) {
// 1.生成pipeline对象
Pipeline pipeline = jedis.pipelined();
// 2.pipeline执行命令,注意此时命令并未真正执行
for (String key : keyList) {pipeline.hgetAll(key);
}
// 3.执行命令 syncAndReturnAll()返回结果
List<Object> objectList = pipeline.syncAndReturnAll();
if (objectList == null || objectList.isEmpty()) {return Collections.emptyMap();
}// 4.解析结果
Map<String,Map<String, String>> resultMap = new HashMap<String, Map<String,String>>();
for (int i = 0; i < objectList.size(); i++) {Object object = objectList.get(i);Map<String, String> map = (Map<String, String>) object;String key = keyList.get(i);resultMap.put(key, map);
}
return resultMap;
}
4.处理人:
修改业务代码。
参考来源:https://yq.aliyun.com/articles/236384?spm=a2c4e.11155435.0.0.e21e2612uQAVoW#cc1
JedisDataException: Please close pipeline or multi block before calling this method相关推荐
- oracle查询导致 gc等待,如何诊断Oracle RAC系统中的等待事件gc cr multi block request?...
AIX上: #no –a udp_recvspace udp_sendspace o 设置udp_sendspace >=[(DB_BLOCK_SIZE * DB_FILE_MULTIBLOCK ...
- cccccccccccccccccccc
http://msdn.microsoft.com/en-us/library/ms972948.aspx This demo is a continuation of my previous exa ...
- redis中的multi和pipeline
事务块 multi redis中的 multi 方法,提供了一个队列用于缓存多个指令,在客户端调用 exec 后将该队列中的指令批量执行,执行过程中不会被其他指令干扰(具有原子性). 客户端每发送一个 ...
- 在 Objective-C 中对 Block 应用 property 时的注意事项
应当使用:@property (nonatomic, copy) 今天在这个问题上犯错误了,找了好久才知道原因. 另外,简单的进行反汇编看了下,Block 被存储在静态变量区,运行时构造出一个运行栈, ...
- Axapta multi records selected and operated:多行操作
Axapta multi records selected and operated:多行操作 Objective: We need to select multi sales order and t ...
- 如何在 iOS 5 中使用 Block
How To Use Blocks in iOS 5 Tutorial – Part 1 How To Use Blocks in iOS 5 Tutorial – Part 2 本人将示范项目放在了 ...
- Python笔记_81_结算页面_订单模型_优惠券
文章目录 结算页面 订单模型 把当前子应用注册到xadmin中 后端实现生成订单的api接口 使用django提供的mysql事务操作保证下单过程中的数据一致性 前端请求生成订单 前端请求后端的订单信 ...
- 魔坊APP项目-23-种植园,宠物和种植物的状态改变、宠物的状态改动
种植园 宠物和种植物的状态改变 1. 宠物的状态改动 2. 种植物的状态改动 3. 道具的使用 宠物的状态改动 因为宠物有多个,每个宠物会有不同的初始生命的饥饿时间,所以我们提前在mysql中进行配置 ...
- 等待事件系列(1)--User I/O类型
等待事件系列(1)--User I/O类型 1 BLOG文档结构图 2 前言部分 2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~ ...
最新文章
- keil git 编译文件_keil下的STM32程序开发部署(一)
- 在线教育音视频质量评价与感知系统
- [mybatis]映射文件_select_resultMap_discriminator鉴别器
- Codeforces 148D:Bag of mice 概率DP
- Glusterfs(distribute) + DRBD + heartbeat + mon 实现分布式文件系统1
- python3.7 倒计时
- c语言标准库详解(一):stdio.h之文件操作
- a5松下驱动器参数设置表_「精品干货」松下A5伺服驱动器参数设置与常见故障解决分析...
- 不打补贴战,快狗打车凭什么冲刺“同城货运第一股”?
- [UE4][Material] Spline样条网格材质消失问题记录
- 免费不限流的内网穿透,外网共享内网文件
- RingAllreduce和NCCL
- 天猫精灵,还缺点啥?
- 2019年,免费微信多开软件哪个好?5款多开软件评测
- OSChina 周六乱弹 —— 作为程序员我对高数的态度
- Android屏幕适配全攻略
- HCIA-DATACOM网络基础
- stm32pid算法控制电机转速_电机控制算法探讨ABB
- Python案例学习:抓取网页表格数据解析并写入Excel
- 设计模式之——享元设计模式