redisTemplate开启事务支持

new SessionCallback() 接口 , 重写 execute方法 保证 在同一个会话中执行所有给定的操作

灰常的 easy , 上代码 !

redisTemplate.execute(new SessionCallback<Object>() {
            @Override
            public Object execute(RedisOperations operations) throws DataAccessException {
                operations.multi(); // 标记事务块的开始
                operations.opsForValue().set("name", "duguotao");
                operations.opsForValue().set("gender", "male");
                operations.opsForValue().set("age", "22");
                return operations.exec(); // 执行以 {multi()} 开始的事务中的所有排队命令。
            }
        });

推荐阅读

Redis分布式下的redis锁

猜你喜欢

转载自blog.csdn.net/weixin_44912855/article/details/113930668