当前位置: 首页 > article >正文

JAVA SpringBoot中使用redis的事务

1、自定义redisConfig , 如果项目中要使用redis事务,最好将用事务和不用事务的redis分开。


@Configuration
public class RedisConfig {

    @Resource
    private RedisProperties redisProperties;


    @Bean("redisTemplate")
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redis1ConnectionFactory());

        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.setHashKeySerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }

    @Bean("redisTransactionTemplate")
    public RedisTemplate<String, Object> redisTransactionTemplate() {
        RedisTemplate<String, Object> redisTransactionTemplate = new RedisTemplate<>();
        redisTransactionTemplate.setConnectionFactory(redis2ConnectionFactory());

        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        redisTransactionTemplate.setKeySerializer(new StringRedisSerializer());
        redisTransactionTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
        redisTransactionTemplate.setHashKeySerializer(genericJackson2JsonRedisSerializer);
        redisTransactionTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        redisTransactionTemplate.afterPropertiesSet();
        //开启事务
        redisTransactionTemplate.setEnableTransactionSupport(true);

        return redisTransactionTemplate;
    }

    @Bean
    @Primary
    public RedisConnectionFactory redis1ConnectionFactory() {
        RedisStandaloneConfiguration localhost = new RedisStandaloneConfiguration(redisProperties.getHost(), redisProperties.getPort());
        if(StrUtil.isNotEmpty(redisProperties.getPassword())){
            localhost.setPassword(redisProperties.getPassword());
        }
        if(Objects.nonNull(redisProperties.getDatabase())){
            localhost.setDatabase(redisProperties.getDatabase());
        }

        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpb =
                (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
        jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
        jedisPoolConfig.setMaxTotal(redisProperties.getMaxActive());
        jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
        jedisPoolConfig.setEvictorShutdownTimeoutMillis(redisProperties.getTimeout());
        jpb.poolConfig(jedisPoolConfig);

        return new JedisConnectionFactory(localhost,jpb.build());
    }

    @Bean
    public RedisConnectionFactory redis2ConnectionFactory() {
        RedisStandaloneConfiguration localhost = new RedisStandaloneConfiguration(redisProperties.getHost(), redisProperties.getPort());
        if(StrUtil.isNotEmpty(redisProperties.getPassword())){
            localhost.setPassword(redisProperties.getPassword());
        }
        if(Objects.nonNull(redisProperties.getDatabase())){
            localhost.setDatabase(redisProperties.getDatabase());
        }

        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpb =
                (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
        jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
        jedisPoolConfig.setMaxTotal(redisProperties.getMaxActive());
        jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());
        jedisPoolConfig.setEvictorShutdownTimeoutMillis(redisProperties.getTimeout());
        jpb.poolConfig(jedisPoolConfig);

        return new JedisConnectionFactory(localhost,jpb.build());
    }

}

@Order(value = 1)
@Data
@Component
public class RedisProperties {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private Integer port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private Integer database;

    @Value("${spring.redis.timeout}")
    private Integer timeout;

    @Value("${spring.redis.lettuce.pool.max-idle}")
    public Integer maxIdle;

    @Value("${spring.redis.lettuce.pool.min-idle}")
    public Integer minIdle;

    @Value("${spring.redis.lettuce.pool.max-active}")
    public Integer maxActive;

    @Value("${spring.redis.lettuce.pool.max-wait}")
    public Integer maxWait;



}

需要事务的注入 

@Resource(name = "redisTransactionTemplate")

不需要事务的注入

@Resource(name = "redisTemplate")

事务完整执行流程

redisTransactionTemplate.multi();   //开启
redisTransactionTemplate.exec();  //执行
redisTransactionTemplate.discard();  //取消


http://www.kler.cn/a/228495.html

相关文章:

  • 《探秘鸿蒙Next:非结构化数据处理与模型轻量化的完美适配》
  • 【二叉树的深搜】计算布尔二叉树的值 求根节点到叶节点数字之和
  • cuda从零开始手搓PB神经网络
  • 【三维分割】Gaga:通过3D感知的 Memory Bank 分组任意高斯
  • 什么样的问题适合用递归
  • 2024又是一年的CSDN之旅-总结过去展望未来
  • vulnhub靶场之Thales
  • vulhub中AppWeb认证绕过漏洞(CVE-2018-8715)
  • 对象内存与方法调用机制
  • Vivado Tri-MAC IP的例化配置(三速以太网IP)
  • ESP32QRCodeReader库使用,ESP32-CAM识别二维码并向自写接口发出请求确认身份。
  • 关于Linux和消息队列常见的十道面试题
  • Verilog实现2进制码与BCD码的互相转换
  • 基于NSGA-II的深度迁移学习
  • 前端实现标题滚动点击导航
  • 爬虫工作量由小到大的思维转变---<第四十五章 Scrapyd 关于gerapy遇到问题>
  • 100个Cocos实例(32/100) 3D模型受击闪白效果简易实现
  • 全网第一篇把Nacos配置中心客户端讲明白的
  • J组一等奖冲刺:原码、反码与补码
  • centos7安装google chrome和chromium
  • SPECCPU2017操作说明
  • MyBatisPlus的链式查询LambdaQueryChainWrapper
  • python常用pandas函数nlargest / nsmallest及其手动实现
  • 【MySQL】DQL的总结和案例学习
  • 开源节点框架STNodeEditor使用
  • 【数字电子技术课程设计】多功能数字电子钟的设计