> MULTI
OK
> INCR foo
QUEUED
> INCR bar
QUEUED
> EXEC
1) (integer) 1
2) (integer) 1
如果事务中出现错误怎么办?
首先我们将事务中的错误分为两类:
进入队列前发现错误,比如命令的语法错误。
EXEC执行后发现错误,比如对同一个key执行的两次不同数据类型的操作。
第一种很好理解,就像上面给出的例子中,成功进入队列后会立马返回"QUEUED"。
如果没能进入队列,则整个事务都会失效,提示"Transaction discarded because of previous errors."
关于EXEC执行后出现的错误,凡是成功进入队列的都会被执行,即便同一事物中有执行失败的命令,其余的命令都会得到执行。
这让那些用过关系型数据库的人们感到诧异, 为什么Redis不支持roll back?
对此官网的两点说法:
Redis commands can fail only if called with a wrong syntax (and the problem is not detectable during the command queueing), or against keys holding the wrong data type: this means that in practical terms a failing command is the result of a programming errors, and a kind of error that is very likely to be detected during development, and not in production.
Redis is internally simplified and faster because it does not need the ability to roll back.