开发者社区> 问答> 正文

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;

上面代码是个人猜想.求大神帮忙

展开
收起
落地花开啦 2016-01-31 18:59:18 4430 0
1 条回答
写回答
取消 提交回答
  • 喜欢技术,喜欢努力的人

    出现异常的位置有两种情况:

    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.

    2019-07-17 18:36:55
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Redis在唯品会的应用实践——架构演进与功能定制 立即下载
微博的Redis定制之路 立即下载
云数据库Redis版的开源之路 立即下载