1、启动redis并登录 redis-cli
127.0.0.1:6379> clear
127.0.0.1:6379> set ticket 1
OK
127.0.0.1:6379> set money 0
OK
127.0.0.1:6379> watch ticket #乐观锁,对值进行观察,改变则事务失败
OK
127.0.0.1:6379> multi #开启事务
OK
127.0.0.1:6379> decr ticket
QUEUED
127.0.0.1:6379> incrby money 100
QUEUED
客户端2:还没等客户端1提交事务,此时客户端2把票买到了。
127.0.0.1:6379> get ticket
"1"
127.0.0.1:6379> decr ticket
(integer) 0
客户端1:
127.0.0.1:6379> exec
(nil) #执行事务,失败
127.0.0.1:6379> get ticket
"0"
127.0.0.1:6379> unwatch #取消监控