zoukankan      html  css  js  c++  java
  • SpringBoot进阶教程(五十六)整合Redis事物

    Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用。这样做会强制将当前重新连接绑定到触发multi的当前线程。如果事务完成时没有出错,则调用exec。否则将调用Discard。一旦进入多个重新连接队列,则写入操作。所有只读操作(如键)都通过管道连接到新的(非线程绑定的)重新连接。

    以上内容来自官网内容的机器翻译。

    SpringBoot进阶教程(二十八)整合Redis事物

    v准备工作

    学习本章节之前,建议依次阅读以下文章,更好的串联全文内容,如已掌握以下列出知识点,请跳过:

    v事物配置

    package com.demo.Redis;
    
    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.StringRedisTemplate;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    /**
     * Created by toutou on 2019/2/16.
     */
    @Configuration
    @EnableTransactionManagement
    public class RedisTranConfig {
        @Bean
        public StringRedisTemplate customStringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
            StringRedisTemplate template = new StringRedisTemplate();
            template.setConnectionFactory(redisConnectionFactory);
            template.setEnableTransactionSupport(true);
            return template;
        }
    }

    v接口测试

        @Autowired
        StringRedisTemplate template;
    
        @Transactional
        @RequestMapping(value = "/testredistran1")
        public String testRedisTran1()
        {
            template.opsForValue().set("test1a", "test1a");
            template.opsForValue().set("test1b", "test1b");
            template.opsForValue().set("test1c", "test1c");
            return "1";
        }
    
        @Transactional
        @RequestMapping(value = "/testredistran2")
        public String testRedisTran2()
        {
            template.opsForValue().set("test2a", "test2a");
            template.opsForValue().set(null, "test2b");
            template.opsForValue().set("test2c", "test2c");
            return "2";
        }

    SpringBoot进阶教程(二十八)整合Redis事物

    分别请求http://localhost:8081/testredistran1和http://localhost:8081/testredistran2,可以发现test2a并没有被写入到Redis中。

    v其它方法

        @RequestMapping(value = "/testredistran3")
        public String testRedisTran3()
        {
            //开启事务
            template.multi();
            template.opsForValue().set("test3a", "test3a");
            template.opsForValue().set(null, "test3b");
            template.opsForValue().set("test3c", "test3c");
            //关闭事务
            template.exec();
            return "3";
        }

     Redis为单进程单线程模式,采用队列模式将并发访问变成串行访问,Redis对事物支持不会很复杂,当一个客服端连接Redis服务时,发出了MULTI命令时,这个连接会进入事物,在执行MULTI命令之后,执行所有的命令都不会执行,会先放到一个队列中,会提示正在Query,当最后执行EXEC命令之后,Redis会按照之前的进入队列的顺序,执行命令。

    v源码地址

    https://github.com/toutouge/javademosecond/tree/master/hellospringboot


    作  者:请叫我头头哥
    出  处:http://www.cnblogs.com/toutou/
    关于作者:专注于基础平台的项目开发。如有问题或建议,请多多赐教!
    版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
    特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是作者坚持原创和持续写作的最大动力!

  • 相关阅读:
    作业
    awk 简单用法小结
    R/SAS/Matab数据相互转换
    Xargs 批量处理命令
    线性表
    C plus 笔记(一 )
    C plus 内存管理
    Skyeye学习(一)—— 安装与使用测试例子
    mongoDB基本操作
    《Algorithms Unlocked》读书笔记3——计数排序
  • 原文地址:https://www.cnblogs.com/toutou/p/redis_transaction.html
Copyright © 2011-2022 走看看