官文: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");
}
}