127.0.0.1:6379> multi # 开启事务 OK 127.0.0.1:6379> set k1 v1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> get k2 QUEUED 127.0.0.1:6379> set k3 v3 QUEUED 127.0.0.1:6379> exec# 执行事务 1) OK 2) OK 3) "v2" 4) OK
放弃事务
1 2 3 4 5 6 7 8 9 10 11 12
127.0.0.1:6379> multi # 开启事务 OK 127.0.0.1:6379> set k1 v1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> set k4 v4 QUEUED 127.0.0.1:6379> discard # 取消事务 OK 127.0.0.1:6379> get k4 # 事务队列中的命令都不会被执行 (nil)
编译型异常,事务中的所有命令都不会被执行!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
127.0.0.1:6379> multi OK 127.0.0.1:6379> set k1 v1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> set k3 v3 QUEUED 127.0.0.1:6379> getset k3 # 错误的命令 (error) ERR wrong number of arguments for'getset'command 127.0.0.1:6379> set k4 v4 QUEUED 127.0.0.1:6379> set k5 v5 QUEUED 127.0.0.1:6379> exec# 执行命令的时候报错 (error) EXECABORT Transaction discarded because of previous errors. 127.0.0.1:6379> get k5 # 所有的命令都不会执行 (nil)
127.0.0.1:6379> multi OK 127.0.0.1:6379> set k1 "v1" QUEUED 127.0.0.1:6379> incr k1 QUEUED 127.0.0.1:6379> set k2 v2 QUEUED 127.0.0.1:6379> set k3 v3 QUEUED 127.0.0.1:6379> get k3 QUEUED 127.0.0.1:6379> exec 1) OK 2) (error) ERR value is not an integer or out of range # 第一条命令报错,但是依旧正常执行成功了! 3) OK 4) OK 5) "v3" 127.0.0.1:6379> get k2 "v2"
127.0.0.1:6379> set money 100 OK 127.0.0.1:6379> set out 0 OK 127.0.0.1:6379> watch money # 监视money对象 OK 127.0.0.1:6379> multi # 事务正常结束,数据期间没有发生变动,这个时候就正常执行成功 OK 127.0.0.1:6379> decrby money 20 QUEUED 127.0.0.1:6379> incrby out 20 QUEUED 127.0.0.1:6379> exec 1) (integer) 80 2) (integer) 20