赞
踩
本文今天主要是Redis的事务的使用,以及redis对应的Java实现该怎么用。因为篇幅问题,我这里写了一个测试类,引入 RedisTemplate对象,后面例子里就不一一引入了。大家理解就行,如果大家还不知道如何通过Spring Boot 整合redis则可以查看我之前的文章:SpringBoot整合redis(redis支持单节点和集群)
package com.alian.datastruct; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; import java.util.concurrent.TimeUnit; @Slf4j @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public class RedisTransactionTest { @Autowired private RedisTemplate<String, Object> redisTemplate; }
一个事务从开始到执行会经历以下三个阶段:
MULTI
命令1
命令2
...
命令n
EXEC
命令操作
127.0.0.1:6379> multi OK 127.0.0.1:6379> set name alian QUEUED 127.0.0.1:6379> set age 28 QUEUED 127.0.0.1:6379> set sex nan QUEUED 127.0.0.1:6379> exec 1) OK 2) OK 3) OK 127.0.0.1:6379> get name "alian" 127.0.0.1:6379> get age "28" 127.0.0.1:6379> get sex "nan"
Java操作
@Test public void startTransaction() { redisTemplate.delete(Arrays.asList("name", "age", "sex")); redisTemplate.execute(new SessionCallback<List<Object>>() { public List<Object> execute(RedisOperations operations) throws DataAccessException { operations.multi(); operations.opsForValue().set("name", "alian"); operations.opsForValue().set("age", "28"); operations.opsForValue().set("sex", "男"); return operations.exec(); } }); log.info("事务提交后的姓名:{}", redisTemplate.opsForValue().get("name")); log.info("事务提交后的年龄:{}", redisTemplate.opsForValue().get("age")); log.info("事务提交后的性别:{}", redisTemplate.opsForValue().get("sex")); }
事务提交后的姓名:alian
事务提交后的年龄:28
事务提交后的性别:男
MULTI
命令1
命令2
...
命令n
DISCARD
命令操作
127.0.0.1:6379> multi OK 127.0.0.1:6379> set orderId 202212230102 QUEUED 127.0.0.1:6379> set title BirthdayCake QUEUED 127.0.0.1:6379> set price 168 QUEUED 127.0.0.1:6379> discard OK 127.0.0.1:6379> get orderId (nil) 127.0.0.1:6379> get title (nil) 127.0.0.1:6379> get price (nil)
Java操作
@Test public void cancelTransaction() { redisTemplate.delete(Arrays.asList("orderId", "title", "price")); redisTemplate.execute(new SessionCallback<List<Object>>() { public List<Object> execute(RedisOperations operations) throws DataAccessException { operations.multi(); operations.opsForValue().set("orderId", "202212230102"); operations.opsForValue().set("title", "BirthdayCake"); operations.opsForValue().set("price", "168"); operations.discard(); return null; } }); log.info("事务取消后的姓名:{}", redisTemplate.opsForValue().get("orderId")); log.info("事务取消后的年龄:{}", redisTemplate.opsForValue().get("title")); log.info("事务取消后的性别:{}", redisTemplate.opsForValue().get("price")); }
事务取消后的姓名:null
事务取消后的年龄:null
事务取消后的性别:null
从这里可以看到,取消事务后,命令都未执行。
一般是命令写错了或者命令不存在导致,比如不存在的命令
命令操作
127.0.0.1:6379> multi OK 127.0.0.1:6379> set name alian QUEUED 127.0.0.1:6379> set age 28 QUEUED 127.0.0.1:6379> setget sex man ##故意写错命令 (error) ERR unknown command `setget`, with args beginning with: `sex`, `man`, 127.0.0.1:6379> set hobby write QUEUED 127.0.0.1:6379> exec (error) EXECABORT Transaction discarded because of previous errors. 127.0.0.1:6379> get name (nil) 127.0.0.1:6379> get age (nil) 127.0.0.1:6379> get hobby (nil)
编译时异常后面的命令都执行失败
一般是类型错误了,比如没有按指定类型获取值,或者是给非数字字符做自增等
命令操作
127.0.0.1:6379> multi OK 127.0.0.1:6379> set title basketball QUEUED 127.0.0.1:6379> set price 239 QUEUED 127.0.0.1:6379> hgetall title ##title的类型是string不是hash QUEUED 127.0.0.1:6379> set address shenzhen QUEUED 127.0.0.1:6379> exec 1) OK 2) OK 3) (error) WRONGTYPE Operation against a key holding the wrong kind of value 4) OK 127.0.0.1:6379> get title "basketball" 127.0.0.1:6379> get price "239" 127.0.0.1:6379> get address "shenzhen"
运行时异常并不影响后面命令的执行。
Redis单条命令是原子性的,但是Redis事务是不保证原子性的,会按照顺序一条条执行
用一个场景来模拟乐观锁。
终端1
127.0.0.1:6379> set money 0
OK
127.0.0.1:6379> set num 10
OK
127.0.0.1:6379> watch money
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> incrby money 100
QUEUED
127.0.0.1:6379> decrby num 1
QUEUED
终端2
127.0.0.1:6379> get money
"0"
127.0.0.1:6379> incrby money 50
(integer) 50
终端1
127.0.0.1:6379> exec
(nil)
127.0.0.1:6379> get money
"50"
127.0.0.1:6379> get num
"10"
正常操作
127.0.0.1:6379> unwatch
OK
127.0.0.1:6379> watch money
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> incrby money 100
QUEUED
127.0.0.1:6379> decrby num 1
QUEUED
127.0.0.1:6379> exec
1) (integer) 150
2) (integer) 9
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。