Exec
Executes the commands within all the transaction blocks.
If a (or some) key is under watch command monitoring and there are commands related to this (or these) key in the transaction block, then the exec command executes and takes effect only if the (or these) key is not modified by another command. Otherwise the transaction is interrupted (abort).
-
-
return value:
-
The return value of all commands within a transaction block, sorted by the order in which they were executed. Null value returned when the operation is interrupted
nil
.
# Monitor key, and the transaction executes successfully 127.0.0.1:6379> get key
"111"
127.0.0.1:6379> Watch Key
Ok
127.0.0.1:6379> Multi
Ok
127.0.0.1:6379> Set Key 222
QUEUED
127.0.0.1:6379> exec
1) OK
127.0.0.1:6379> Get key
"222"
127.0.0.1:6379> # Monitor key, and transaction is interrupted 127.0.0.1:6379> set key 222
QUEUED
127.0.0.1:6379> exec
1) OK
127.0.0.1:6379> Get key
"222"
127.0.0.1:6379> Watch Key
Ok
127.0.0.1:6379> Set Key 333
Ok
127.0.0.1:6379> Multi
Ok
127.0.0.1:6379> Set Key 444
QUEUED
127.0.0.1:6379> exec
(nil)
"333"
------------------
Under the same client, as long as the value of key is changed before multi executes after watch, the changed value will be the current key value, and the operation on changing the key value in the transaction will no longer take effect.
Under different clients, for example, perform watch key on a client and then execute multi. At this point, under the B client re-set the key value, and then back to the a client, also reset another set key value, then exec will return to a null value, the final key value is the value set under the B client.
In theory, a client will eventually set the value in the transaction to the final value, but the a client will assume that the value of the B client was executed before multi, and then the a client execution will return empty.
Redis Watch multi exec Relationship