背景:

项目中遇到有一系列对Redis的操作,并需要保持事务处理。

环境:

Spring version 4.1.8.RELEASE

Redis Server 2.6.12 (64位)

spring-data-redis version 1.6.1.RELEASE

jedis version 2.7.3

使用Spring的@Transactional对redis操作进行控制

Spring Redis的配置文件如下:

<description>Jedis配置</description>

    <!-- Redis 配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:hostName="${redis.host}" p:port="${redis.port}" p:usePool="true"
p:timeout="${redis.timeout}" p:password="${redis.pass}" p:database="${redis.default.db}"
p:poolConfig-ref="jedisPoolConfig" /> <bean id="stringRedisSerializer"
class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"
p:defaultSerializer-ref="stringRedisSerializer"
p:enableTransactionSupport="false" /> <bean id="redisTemplateTransactional" class="org.springframework.data.redis.core.RedisTemplate"
p:connection-factory-ref="jedisConnectionFactory"
p:defaultSerializer-ref="stringRedisSerializer"
p:enableTransactionSupport="true" />

其中的p:enableTransactionSupport 需要设置为true,才能开启Redis事务管理控制。

业务代码如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; @Autowired
private RedisTemplate<String, String> redisTemplateTransactional; @Transactional
public void transactionalForRedis() throws Exception {   redisTemplateTransactional.boundValueOps("name").set("TEST_XXX");
  redisTemplateTransactional.boundValueOps("age").set("11");

  // 异常代码
  for (int i = 0; i < 5; i++) {
    if (i == 3) {
      throw new RedisSystemException("dsd", new Exception("myself exception....."));
    }
  }
}

1.使用Spring的事务控制即可:@Transactional

2.其中会抛出异常处理,必须抛出RedisSystemException这个异常才能回滚,如果是自己封装的异常类,不能回滚。

下面是结合数据库操作与Redis,在同一个事务中进行控制的代码样例:

@Transactional
public void test() throws Exception { //操作Redis
redisTemplateTransactional.boundValueOps("name").set("TEST_YYYY");
redisTemplateTransactional.boundValueOps("age").set("80"); //操作数据库: JPA保存操作
SimpleDemoEntity simpleDemoEntity = new SimpleDemoEntity();
simpleDemoEntity.setRemark("nima");
simpleDemoEntity.setWeixinId("1000");
simpleDemoDao.saveAndFlush(simpleDemoEntity);
}

当遇到数据库异常情况,比如,主键冲突,唯一索引值等数据库异常,Redis的操作也可以正常回滚。

原因:

使用@Transactional 事务控制后,Spring对数据库层面进行了完美的事务控制,那么当数据库层抛出的所有异常都可以被事务控制管理。

而Redis或者使用Jedis本身的异常,或者业务异常类并没有被Spring的事务控制管理。
而Spring-redis,给了我们一个可以抛出业务异常的渠道,那就是RedisSystemException。

Spring Framework 中启动 Redis 事务操作的更多相关文章

  1. Spring Framework中常见的事务传播陷阱(译文)

    最近看到Medium上一篇讨论Spring Framework中事务传播的文章,解释了几种常见的问题,解释的不错,这里直接翻译吧(意译为主,粗体和斜体是我自己加上的). 译文: 这是我的第一篇文章,我 ...

  2. 【redis】spring boot中 使用redis hash 操作 --- 之 使用redis实现库存的并发有序操作

    示例: @Autowired StringRedisTemplate redisTemplate; @Override public void dealRedis(Dealer dealer) { d ...

  3. 转-Spring Framework中的AOP之around通知

    Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: spring ...

  4. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->关于spring framework中的beans

    Spring framework中的beans 1.概述 bean其实就是各个类实例化后的对象,即objects spring framework的IOC容器所管理的基本单元就是bean spring ...

  5. Redis事务操作

    Redis事务操作 Redis事务本质: ​ 一组命令的集合 , 一个事务中的所有命令都会被序列化 , 在事务执行过程中 , 会按照顺序执行 一次性 : 事务之间的事情,会一次性执行,而不是立刻执行 ...

  6. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块

    spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...

  7. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中web相关的知识(概述)

    Spring Framework中web相关的知识 1.概述: 参考资料:官网documentation中第22小节内容 关于spring web mvc:  spring framework中拥有自 ...

  8. spring boot 中使用redis session

    spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...

  9. Spring Boot中使用Redis小结

    Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, 等. Redis简单介绍 Redi ...

随机推荐

  1. 如何发布使用LGPL版Qt的商业软件

    最近做跨平台图形用户界面库选型,权衡很多因素后最终选择了Qt,其中一个重要因素就是Qt使用LGPL授权许可.由于本人对LGPL理解有限,始终对闭源商业软件如何发布Qt库存在疑问,其中最关心的是:发布的 ...

  2. JS中用execCommand("SaveAs")保存页面兼容性问题解决方案

    开发环境:ASP.NET MVC,其他环境仅供参考. 问题描述:在开发中遇到这样的需求,保存页面,通常使用JavaScript的saveAs进行保存,各浏览器对saveAs支持,见下表. 代码一:初始 ...

  3. 244. Shortest Word Distance II

    题目: This is a follow up of Shortest Word Distance. The only difference is now you are given the list ...

  4. float与double的范围和精度

    1. 范围   float和double的范围是由指数的位数来决定的.  float的指数位有8位,而double的指数位有11位,分布如下:  float:  1bit(符号位) 8bits(指数位 ...

  5. iOS处理XMl提供GDataXMLNode下载的链接

    GDataXMLNode .好东西,处理xml 在iOS 中使用.可以编辑和读取Xml文档.支持Xpath.这个很好. GDataXMLNode.h GDataXMLNode.m 文件很不好找啊. / ...

  6. tengine lua 开源一 调用内部接口高效发送文件

    tengine  lua 开源一 调用内部接口高效发送文件 开源自己封装的sendfile 模块,可以高效的通过lua发送文件 源码地址:https://github.com/weinyzhou/Lu ...

  7. Android动画 三种动画

    Android可以使用三种动画 Frame Animation-帧动画 ,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果 Tween Animation-补间动画,给出两个关键帧, ...

  8. Codeforces 672

    题目链接:http://codeforces.com/contest/672/problem A. Summer Camp(打表) 题意:123456789...一串字符串,问第n个是什么数字. 塞一 ...

  9. 转:Cache相关

    声明:本文截取自http://blog.163.com/ac_victory/blog/static/1033187262010325113928577/ (1)“Cache”是什么 Cache(即高 ...

  10. 数据库MySQL-Oracle-DB2-SQLServer分页查询

    1. MySQL分页查询 (1)关键字: LIMIT beginIndex, maxRow (2)示例: LIMIT子句可以用来限制由SELECT语句返回过来的数据数量,它有一个或两个参数. 如果给出 ...