「已注销」 2015-10-14 07:30 采纳率: 100%
浏览 3571
已采纳

redis写入多条数据.排队列的时候有了异常.该怎么处理

如果
public void test2Trans() {
long start = System.currentTimeMillis();
Transaction tx = jedis.multi();
try{
for (int i = 0; i < 100000; i++) {
tx.set("t" + i, "t" + i);
}
}catch(Exception e){
tx.discard();
}
List results = tx.exec();
long end = System.currentTimeMillis();
System.out.println("Transaction SET: " + ((end - start)/1000.0) + " seconds");
}

    假设代码在try{}模块出现问题,那么catch{}模块直接取消事务的话.那么如何让这段代码实现rollback;
    上面代码是个人猜想.求大神帮忙
  • 写回答

1条回答 默认 最新

  • jituotianxia2009 2015-10-14 11:34
    关注

    出现异常的位置有两种情况:
    1. 在exec之前;
    2. 在exec执行时;
    在exec之前出现异常,使用discard可以rollback,
    在exec执行中时,即使出现异常,redis也不会终止和rollback,而是继续执行。
    官方这样解释不支持回滚:
    Redis is internally simplified and faster because it does not need the ability to roll back.
    An argument against Redis point of view is that bugs happen, however it should be noted that in general the roll back does not save you from programming errors. For instance if a query increments a key by 2 instead of 1, or increments the wrong key, there is no way for a rollback mechanism to help. Given that no one can save the programmer from his errors, and that the kind of errors required for a Redis command to fail are unlikely to enter in production, we selected the simpler and faster approach of not supporting roll backs on errors.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 1月4日

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向