问题描述
- 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. 在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.