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

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日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格