SpringBoot(spring-data-redis)对事物的支持

本文介绍如何使用Spring Data Redis进行事务管理,包括两种方案:使用RedisTemplate的execute方法结合回调函数,以及使用Spring的事务注解自动管理。通过具体代码示例展示了如何配置RedisTemplate以支持事务,并给出了Controller层调用Service层的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官文:https://docs.spring.io/spring-data/data-redis/docs/current/reference/html/#redis:template

虽然RedisTemplate提供了multi/exec/discard方法,但是按照理解的方式直接在代码前后调用multi和exec会抛出异常(ERR EXEC without MULTI). 下面说一下官方调用姿势。

Spring-data-redis对于事物支持提供了两种方案:

    方案1:使用redisTemplate.execute(callback...), 通过实现callback来实现一组操作在一个connection内。

List<Object> txResults = redisTemplate.execute(new SessionCallback<List<Object>>() {
  public List<Object> execute(RedisOperations operations) throws DataAccessException {
    operations.multi();
    operations.opsForSet().add("key1", "value1");
    operations.opsForSet().add("key2", "value2");
    return operations.exec();
  }
});

    方案2:使用spring的事物注解自动管理功能, 实现如下:

package com.demo.api.redis;

import java.nio.charset.Charset;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author tom
 * redis支持事物
 */
@Configuration
@EnableTransactionManagement //第一步 :开启事物管理
public class RedisTemplateConfiguration {

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

        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);

        Jackson2JsonRedisSerializer<Object> jacksonSeial = new Jackson2JsonRedisSerializer<Object>(Object.class);

        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSeial.setObjectMapper(om);

        redisTemplate.setValueSerializer(jacksonSeial);
        redisTemplate.setKeySerializer(new StringRedisSerializer(Charset.forName("UTF-8")));

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(jacksonSeial);
        //support transaction 第二步:强制支持事物
        redisTemplate.setEnableTransactionSupport(true);
        
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
    //第三步:定义事务管理bean,就是jdbc里面的transactionManager
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) throws SQLException {
      return new DataSourceTransactionManager(dataSource);   
    }
    
    @Bean
    public DataSource dataSource() {
    	return DruidDataSourceBuilder.create().build();
    }
	
}
            

然后在controller里面调用下面的service,试一下事物的效果,然后在redis里看一下值是否设置成功。(老衲测得是可以的)

package com.demo.api.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class RedisService {

	@Autowired
	private RedisTemplate<String, Object> redis;
	
	@Transactional
	public void testOk() {
		redis.opsForValue().set("a", "aa");
		redis.opsForValue().set("b", "cc");
	}
	
	@Transactional
	public void testError() {
		redis.opsForValue().set("d", "aa");
		redis.opsForValue().set(null, "cc");//抛出异常
		redis.opsForValue().set("f", "cc");
	}
	
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值