Spring Framework 中启动 Redis 事务操作
背景:
项目中遇到有一系列对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 事务操作的更多相关文章
- Spring Framework中常见的事务传播陷阱(译文)
最近看到Medium上一篇讨论Spring Framework中事务传播的文章,解释了几种常见的问题,解释的不错,这里直接翻译吧(意译为主,粗体和斜体是我自己加上的). 译文: 这是我的第一篇文章,我 ...
- 【redis】spring boot中 使用redis hash 操作 --- 之 使用redis实现库存的并发有序操作
示例: @Autowired StringRedisTemplate redisTemplate; @Override public void dealRedis(Dealer dealer) { d ...
- 转-Spring Framework中的AOP之around通知
Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: spring ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->关于spring framework中的beans
Spring framework中的beans 1.概述 bean其实就是各个类实例化后的对象,即objects spring framework的IOC容器所管理的基本单元就是bean spring ...
- Redis事务操作
Redis事务操作 Redis事务本质: 一组命令的集合 , 一个事务中的所有命令都会被序列化 , 在事务执行过程中 , 会按照顺序执行 一次性 : 事务之间的事情,会一次性执行,而不是立刻执行 ...
- 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架构模式,可 ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中web相关的知识(概述)
Spring Framework中web相关的知识 1.概述: 参考资料:官网documentation中第22小节内容 关于spring web mvc: spring framework中拥有自 ...
- spring boot 中使用redis session
spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...
- Spring Boot中使用Redis小结
Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, 等. Redis简单介绍 Redi ...
随机推荐
- pancake sort的几个问题
1. 每次找剩下序列中的最大值,可以进行pancake sort,时间复杂度是O(n^2): 2. 求最少交换次数进行pancake sort的问题是个NP问题,搜索的时候,upper bound是2 ...
- Topcoder 练习小记,Java 与 Python 分别实现。
Topcoder上的一道题目,题目描述如下: Problem Statement Byteland is a city with many skyscrapers, so it's a pe ...
- netty 解决TCP粘包与拆包问题(三)
今天使用netty的固定长度进行解码 固定长度解码的原理就是按照指定消息的长度对消息自动解码. 在netty实现中,只需要采用FiexedLengthFrameDecoder解码器即可... 以下是服 ...
- WC约束示使用
1.接口中添加时要注意Xml序列化标签不要随意添加啊.[webInvoke].[OpertorContract]这2个约束就行了.不然,直接坑死啊.
- Oracle默认的用户名和密码
你是说默认的用户名和密码么scott 密码是 tigersys 密码是 change_on_installsystem 密码是 managersysman 密码是 oem_temp 其中直接管理模式可 ...
- sublime3 常用功能总结
介绍几个常见的功能: l 自动完成:自动完成的快捷键是Tab和Enter,如果在html文件中,输入cl按下tab或Enter,即可自动补全为class=””:加上zencoding后,更是如虎添翼, ...
- 【 D3.js 视频系列 】 飞速入门
本教程共包含 6 个视频,目的是为了帮助初学者快速入门,以便阅读本站其他文章. 本教程的名称为"飞速入门",是为初学者准备的,其中包括了 D3 开发中最基础的知识.对 D3 掌握得 ...
- PS流格式
概念: 将具有共同时间基准的一个或多个PES组合(复合)而成的单一的数据流称为节目流(Program Stream). ES是直接从编码器出来的数据流,可以是编码过的视频数据流,音频数据流,或其他编码 ...
- getDeclaredMethods()和getMethods()区别
getDeclaredMethods() 返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,包括公共.保护.默认(包)访问和私有方法, ...
- H264相关知识
1.基本概念 I frame :帧内编码帧 又称intra picture,I 帧通常是每个 GOP(MPEG 所使用的一种视频压缩技术)的第一个帧,经过适度地压缩,做为随机访问的参考点,可以当成图象 ...