Redis - 事务及Springboot应用

Redis事务

Redis每一个命令都是原子性的

事务可以一次执行多个命令(顺序的串行执行)

  • Redis会将一个事务中所有命令序列化,顺序执行,不会被其他命令插入,不许出现加塞现象
  • Redis没有在事务上增加任何维持原子性的机制,所以Redis事务的执行并不是原子性的
  • Redis事务不支持回滚

命令

  • multi标记开始一个事务块

  • exec执行事务块的命令

  • discard 取消事务,放弃执行事务块命令

  • watch key[key...]监视一个或多个key,在事务执行前该key被其他命令改动,打断事务

  • unwatch取消watch对key的监视


事务执行

事务执行有3个阶段:

  1. 开始事务块
  2. 命令加入队列
  3. 执行事务块

a账户100元,b账户50元
在这里插入图片描述

当前事务为a向b转账50元:queued表示该命令加入队列

在这里插入图片描述


discard

discard取消事务块:

在这里插入图片描述

当前数据库并没有aa,get aa也没有执行:
在这里插入图片描述

可以知道:

  • 从multi开始事务块,命令依次加入队列,并不会执行,直到输入exec才会执行事务块
  • discard清除事务队列,然后退出事务

事务:错误处理

  1. 因为事务的执行并不是原子操作,当事务中某个命令执行错误(语法问题),只有错误的命令不会执行,其他都会执行,不会回滚

在这里插入图片描述

  1. 当命令错误时,直接退出事务

前面是事务中命令执行错误,事务依旧执行,而当事务中命令是错误的,无法加入事务队列,会退出事务

在这里插入图片描述


watch

watch用来监视key
当执行事务的过程中,key发生改变:

num为102,事务为设置num为200并加一
在这里插入图片描述

当事务执行前,另一个操作把num设置为了300
在这里插入图片描述

最终执行结果num还是为201

在这里插入图片描述

这明显是不符合要求的,num=300无效了

数据库中提供了 锁(lock) 来解决这一问题,redis使用watch监视

watch a,监视a是否变化

在事务执行之前,在另一个客户端set a 999
在这里插入图片描述

该事务不会执行(null)

在这里插入图片描述


Springboot中Redis事务的运用

项目:

在这里插入图片描述

pom.xml依赖:springboot-redis内置了lettuce,commons-pool2

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--redis依赖commons-pool-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>

RedisConfig:设置序列化策略Jackson2JsonRedisSerializer,将pojo实例序列化成json格式存储在redis中(不设置的话会使用使用JDK本身序列化机制,不好阅读)
在这里插入图片描述

package transaction.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.sql.DataSource;

/**
 * Author : zfk
 * Data : 17:21
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory factory){


        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer jacksonSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jacksonSerializer.setObjectMapper(objectMapper);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        
        //key采用String的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        //value序列化方式采用Jackson
        redisTemplate.setValueSerializer(jacksonSerializer);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }


}

application.yml配置redis连接信息:

在这里插入图片描述

测试类:

事务是命令队列,我们可以用SessionCallback把多个命令放入到同一个Redis连接中去执行

package transaction;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.List;

@SpringBootTest
class DemoApplicationTests {


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Resource(name = "redisTemplate")
    private ValueOperations<String, Object> valueOps;


    @Test
    public void contextLoads() {


        valueOps.set("key1","value1");

        List list = (List) redisTemplate.execute(new SessionCallback() {

            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                // 设置要监控key1
                operations.watch("key1");
                // 开启事务,在exec命令执行前,进入队列
                operations.multi();
                operations.opsForValue().set("key2", "value2");
                operations.opsForValue().set("key1", "update_value1");

                // 获取值将为null,因为redis只是把命令放入队列,
                Object value2 = operations.opsForValue().get("key2");
                System.out.println("命令在队列,所以value为null【" + value2 + "】");

                operations.opsForValue().set("key3", "value3");
                Object value3 = operations.opsForValue().get("key3");
                System.out.println("命令在队列,所以value为null【" + value3 + "】");

                // 执行exec命令,将先判别key1是否在监控后被修改过,如果是不执行事务,否则执行事务
                return operations.exec();
            }

        });

        list.stream().forEach(System.out::println);
    }

}

在这里插入图片描述

在Redis中确定存在:

在这里插入图片描述

注意:事务报错一样会发生在Springboot中,且会影响后续Java语句的执行

在Redis中语法错误不会影响事务的执行:
在这里插入图片描述

例如:添加一个increment的语法错误

在这里插入图片描述

报错:

org.springframework.data.redis.RedisSystemException: 
Error in execution; nested exception is io.lettuce.core.RedisCommandExecutionException:
ERR value is not an integer or out of range

在这里插入图片描述

后续的打印list语句失败,但事务执行成功


代码

我的码云,有兴趣可以查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值