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

springboot中redis的事务的研究

redis的事务类似于队列操作,执行过程分为三步:

  1. 开启事务
  2. 入队操作
  3. 执行事务

使用到的几个命令如下:

命令说明
multi开启一个事务
exec事务提交
discard事务回滚
watch监听key(s):当监听一个key(s)时,如果在本次事务提交之前,有其他命令修改了该key的值,那么本地事务就会失效
unwatch取消监听key(s)

下面我们使用一个springboot的代码操作来说明这几个命令的含义:

package com.test.spring;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class TestSpringApplication {@Autowiredprivate StringRedisTemplate stringRedisTemplate;@RequestMapping("/exec1")public String test1(){stringRedisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {connection.multi();connection.commands().set("k1".getBytes(),"1".getBytes());connection.commands().set("k2".getBytes(),"2".getBytes());connection.exec();return true;}});return "k1="+stringRedisTemplate.opsForValue().get("k1")+",k2="+stringRedisTemplate.opsForValue().get("k2");}@RequestMapping("/exec2")public String exec2(){try{stringRedisTemplate.execute(new RedisCallback<Boolean>() {private int i=0;@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {connection.multi();connection.commands().set("k1".getBytes(),"11".getBytes());connection.commands().set("k2".getBytes(),"22".getBytes());if(i==0){throw new RedisSystemException("一个异常",new RuntimeException("1"));}connection.exec();return true;}});}catch (Exception e){e.printStackTrace();}return "k1="+stringRedisTemplate.opsForValue().get("k1")+",k2="+stringRedisTemplate.opsForValue().get("k2");}@RequestMapping("/discard")public String discard(){try{stringRedisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {connection.multi();connection.commands().set("k3".getBytes(),"3".getBytes());connection.commands().set("k4".getBytes(),"4".getBytes());connection.discard();connection.exec();return true;}});}catch (Exception e){e.printStackTrace();}return "k3="+stringRedisTemplate.opsForValue().get("k3")+",k4="+stringRedisTemplate.opsForValue().get("k4");}@RequestMapping("/watch1")public String watch1(){//开启一个线程new Thread(()->{stringRedisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {try {connection.watch("k5".getBytes());connection.multi();connection.commands().set("k5".getBytes(), "5".getBytes());//休眠5秒钟在提交事务try {Thread.sleep(5000);} catch (InterruptedException e) {}connection.exec();}catch (Exception e){e.printStackTrace();}return true;}});}).start();//开启一个线程new Thread(()->{stringRedisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {try {connection.multi();connection.commands().set("k5".getBytes(), "55".getBytes());connection.exec();}catch (Exception e){e.printStackTrace();}return true;}});}).start();return "success";}@RequestMapping("/watch2")public String watch2(){return "k5="+stringRedisTemplate.opsForValue().get("k5");}public static void main(String[] args) {SpringApplication.run(TestSpringApplication.class, args);}}
  1. exec1
    正常流程,使用curl进行测试会返回:k1=1,k2=2
  2. exec2
    我们模拟在事务队列中发送异常,会发现这段设值不成功,测试返回:k1=1,k2=2
  3. discard
    事务回滚,我们先回滚,再提交,后台会抛出:ERR EXEC without MULTI错误,说明设值失败
  4. watch1、watch2
    这里我们模拟两个线程,第一个线程先监听key,然后等待5秒钟,但是第二个线程直接去修改这个key,当5秒结束时,第一个线程再去提交事务时,会发现已经失效了,然后我们再通过watch2去查询值,测试返回:k5=55,说明线程1事务失效

最后再说明一下unwatch,每次操作exec()后,底层会自动调用unwatch,所以我们可以不用显示去调用unwatch命令。

http://www.lryc.cn/news/2385735.html

相关文章:

  • 为什么我输入对了密码,还是不能用 su 切换到 root?
  • client.chat.completions.create方法参数详解
  • 量子计算与云计算的融合:技术前沿与应用前景
  • 《企业级日志该怎么打?Java日志规范、分层设计与埋点实践》
  • python模块管理环境变量
  • 【泛微系统】后端开发Action常用方法
  • 【算法】力扣体系分类
  • sql:如何查询一个数据表字段:Scrp 数据不为空?
  • 深入浅出人工智能:机器学习、深度学习、强化学习原理详解与对比!
  • 索引下探(Index Condition Pushdown,简称ICP)
  • 基于 ColBERT 框架的后交互 (late interaction) 模型速递:Reason-ModernColBERT
  • vector中reserve导致的析构函数问题
  • 微软开源多智能体自定义自动化工作流系统:构建企业级AI驱动的智能引擎
  • 关于vector、queue、list哪边是front、哪边是back,增加、删除元素操作
  • KubeVela入门到精通-K8S多集群交付
  • RocketMq的消息类型及代码案例
  • Eigen 直线拟合/曲线拟合/圆拟合/椭圆拟合
  • 安卓无障碍脚本开发全教程
  • svn迁移到git保留记录和Python字符串格式化 f-string的进化历程
  • SOC-ESP32S3部分:10-GPIO中断按键中断实现
  • 【神经网络与深度学习】扩散模型之原理解释
  • 语音合成之十六 语音合成(TTS)跳跃与重复问题的解析:成因、机制及解决方案
  • 战略-2.1 -战略分析(PEST/五力模型/成功关键因素)
  • python第三方库安装错位
  • 如何把vue项目部署在nginx上
  • Vue3集成Element Plus完整指南:从安装到主题定制下-实现后台管理系统框架搭建
  • SpringBoot项目配置文件、yml配置文件
  • Linux性能监控:工具与最佳实践
  • windows11 安装 jupyter lab
  • 【算法】:动态规划--背包问题