Redis事务
Redis事务是一组命令的集合,也是Redis的最小执行单位之一。一个事务的所有命令,要么都执行,要么都不执行。Redis能保证事务执行期间不会有其他命令插入。redis事务实现使用乐观锁方式。
相关命令
命令 | 格式 | 说明 |
---|---|---|
DISCARD | DISCARD | 取消事务 |
EXEC | EXEC | 执行事务中的命令 |
MULTI | MULTI | 标记一个事务的开始 |
UNWATCH | UNWATCH | 取消对key的监视 |
WATCH | WATCH key [key ...] | 监视一个或多个key |
执行MULTI后,在EXEC或DISCARD之前发送的所有命令都会被Redis按顺序缓存起来,并返回QUEUED。执行EXEC后,Redis取出缓存的命令开始执行,并返回每一条命令的返回值。
使用WATCH监控的key如果发生了变化,事务将被打断。在EXEC或DISCARD执行后,WATCH监控自动失效,否则使用UNWATCH取消监控。
注意:
①执行MULTI后不能再执行WATCH,否则返回错误“WATCH inside MULTI is not allowed”;
②如果命令有语法错误Redis会报错,但是运行时出现的错误会被Redis忽略不影响后面的执行,需要自己确保不出现运行时错误;
使用方法
- 事务正常执行
192.168.1.100:6379> multi
OK
192.168.1.100:6379> set testkey 1
QUEUED
192.168.1.100:6379> incr testkey
QUEUED
192.168.1.100:6379> get testkey
QUEUED
192.168.1.100:6379> exec
1) OK
2) (integer) 2
3) "2"
- WATCH监控的key变化,事务被打断
192.168.1.100:6379> watch testkey
OK
192.168.1.100:6379> multi
OK
192.168.1.100:6379> set testkey 1
QUEUED
# 此时另一个客户端执行 set testkey 2 改变了testkey的值
192.168.1.100:6379> exec
(nil)
jedis使用代码示例
public class TransactionTest {
private static final String transactionRedisCommitKey = "tansactionCommitKey";
private static final String transactionRedisRollbackKey = "tansactionRollbackKey";
public static void main(String[] args) {
JedisPool pool = new JedisPool(new JedisPoolConfig(), "192.168.222.188",6379);
//模拟两个客户端
Jedis jedisA = pool.getResource();
Jedis jedisB = pool.getResource();
//事务提交,transactionRedisCommitKey变为2
jedisA.set(transactionRedisCommitKey,"hello");
jedisA.watch(transactionRedisCommitKey);
Transaction transaction =jedisA.multi();
transaction.set(transactionRedisCommitKey,"1");
transaction.incr(transactionRedisCommitKey);
transaction.exec();
//事务回滚,transactionRedisRollbackKey依然为hello
jedisB.set(transactionRedisRollbackKey,"hello");
jedisB.watch(transactionRedisRollbackKey);
Transaction transactionB =jedisB.multi();
transactionB.set(transactionRedisRollbackKey,"1");
transactionB.incr(transactionRedisRollbackKey);
transactionB.discard();
jedisB.close();
//当exec时候如果监视的key从调用watch后发生过变化,则整个事务会失败(乐观锁)
Jedis jedisC = pool.getResource();
Jedis jedisD = pool.getResource();
jedisC.watch("test");
Transaction transactionC =jedisC.multi();
transactionC.set("test","transactionC");
//改变被监控的值,导致事务失败
jedisD.set("test","transactionD");
transactionC.exec();
}
}