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 ...
随机推荐
- NPOI基础入门(旧版本)
1.常用的类与方法 工作本HSSFWorkbook 构造方法,无参表示创建一个新的工作本,可以接收一个流用于打开一个现有的工作本 方法CreateSheet(索引):创建指定索引的sheet对象 方法 ...
- Linux autoconf和automake使用
作为Linux下的程序开发人员,一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的Mak ...
- Map.putAll方法——追加另一个Map对象到当前Map集合(转)
该方法用来追加另一个Map对象到当前Map集合对象,它会把另一个Map集合对象中的所有内容添加到当前Map集合对象. 语法 putAll(Map<? extends K,? extends V ...
- 英文论文写作之讨论与结论Discussion and Conclusion
Discussion and Conclusion After viewing these maps, what should immediately appear is the level of r ...
- 即时通信Spark安装和配置
spark:Cross-platform real-time collaboration client optimized for business and organizations.Spark i ...
- HDU 3038 How Many Answers Are Wrong 并查集带权路径压缩
思路跟 LA 6187 完全一样. 我是乍一看没反应过来这是个并查集,知道之后就好做了. d[i]代表节点 i 到根节点的距离,即每次的sum. #include <cstdio> #in ...
- redis sentinel 配置
在最小配置:master.slave各一个节点的情况下,不管是master还是slave down掉一个,“完整的”读/写功能都将受影响,这在生产环境中显然不能接受.幸好redis提供了sentine ...
- Android 下log的使用总结
Android 下log的使用总结 一:在源码开发模式下 1:包含头文件: #include <cutils/log.h> 2:定义宏LOG_TAG #define LOG_TAG &qu ...
- 注意:C++中double的表示是有误差的
注意:C++中double的表示是有误差的,直接通过下面的例子看一下 #include<iostream> using namespace std; int main() { double ...
- GIT使用教程与基本原理
转自:http://blog.csdn.net/wengpingbo/article/details/8985132 说明:该教程全部图片都来自于<pro git>.以下所有的操作,除非特 ...