SpringBoot项目中使用Redis事务出现ERR EXEC without MULTI的分析与总结

一、场景

虚拟机CentOS7

redis版本6.2.6

SpringBoot版本2.5.6

spring-boot-starter-data-redis依赖版本2.4.0

配置文件application.properties

#Springboot整合redis单机配置

# Redis服务器地址
spring.redis.host=192.168.52.128
#Redis服务器连接端口
spring.redis.port=6379
#Redis数据库索引(默认为0)
spring.redis.database=0
#连接超时时间(毫秒)
spring.redis.timeout=1800000ms
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=8
#最大阻塞等待时间(负值表示没有限制)
spring.redis.lettuce.pool.max-wait=-1ms
#连接池中最大空闲连接
spring.redis.lettuce.pool.max-idle=6
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=5

RedisTemplate配置

package com.example.redistest.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.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setConnectionFactory(factory);
        // key 序列化方式
        template.setKeySerializer(redisSerializer);
        // value 序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // value hashmap序列化方式
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        // 开启事务支持
        template.setEnableTransactionSupport(true);
        return template;
    }

}

简单的使用redis事务的代码,测试代码如下:

package com.example.redistest.test;

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


@SpringBootTest
public class RedisTxTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void txTest() {
        try {
            redisTemplate.multi();
            redisTemplate.opsForValue().set("tx", "test");
            redisTemplate.exec();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String tx = (String) redisTemplate.opsForValue().get("tx");
        System.out.println(tx);
    }
}

执行后出现异常,异常信息如下:

上面的控制台日志提示是在执行redisTemplate.exec();之前没有执行multi指令标记一个事务块的开始。这里有两个诡异之处:

1、明明在执行redisTemplate.exec();之前执行了redisTemplate.multi();,为啥提示没有先执行multi呢?

2、对redis的操作在exec之后一起执行的,该处exec执行报错,按理来说往redis中插入key为tx,value为test的键值对不应该会成功。但是为什么执行成功了?

在网上查找了好多关于ERR EXEC without MULTI报错的文章,基本上都是说开始redis的事务,不管是在配置redisTemplate时还是在执行redisTemplate.multi();前加上template.setEnableTransactionSupport(true);。都试了个遍,没有用,依然报这个错。

二、探究源码 

在网上苦询无果后,只能看源码了。

首先,执行redisTemplate.multi();开始一个事务时会执行exectue方法,

 execute方法,注意此处的finally,表示执行完后会释放该redis连接。

 通过该方法关闭redis连接。

 关闭连接

 

 注意,此处很关键,由于此时,pool非null,所以此时执行这样一个方法this.discardIfNecessary(connection);在该方法中需要先判断该redis连接如果是multi则需要执行discard()方法,该redis连接会通过该方法执行discard相关指令,其中个multi.cancel()方法取消队列中的操作

 三、分析总结

通过上面对源码的探究不难发现,在执行redisTemplate.multi();开启一个事务块后,因为当前连接是multi操作,在释放连接前会执行discard的。而discard指令用于刷新一个事务中所有在排队等待的指令,并且将连接状态恢复到正常。此时队列中无任何指令,直接退出该事务块。相当于我开启了一个事务块,啥也没做然后退出来并且关闭了此次连接。然后继续执行后面的插入key为tx,value为test的操作。此时该操作是不在任何事务当中的,并且可以执行成功。而后执行redisTemplate.exec();时该连接当然没有先执行multi指定,而是直接exec指令,所以才会报错。至于源码为啥会是这种逻辑,我也不知道是为啥,总之直接使用这样的方式是不可以操作redis的事务的。

可以通过SessionCallback接口或@Transactional来使用redis的事务。具体操作方式可以参考官网描述。

使用SessionCallback接口操作redis事务

RedisTemplate对@Transactional的支持

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luffylv

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值