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 ...
随机推荐
- 61. Rotate List
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...
- Filter设计实现IP地址限制
示例:创建一个IP过滤Filter,当一个用户发出访问请求的时候,首先通过过滤器进行判断, 如果用户的IP地址被限制,就禁止访问,只有合法的IP才可以继续访问.IP过滤Filter代码如下: IPFi ...
- YASKAWA电机控制(1)---接线
实验室所购置电机型号为YASKAWA-AC SERVO MOTOR SGM7J-01AFC6S型,配SGD7S-R90A00A002伺服控制器.电机和控制器的操作说明书由安川中文官网安川电机资料提供. ...
- 一个小应用的dbcp和c3p0配置实例
以下是一个小应用的数据库连接池配置,包括DBCP和C3P0的配制方法 因为是小应用,完全不涉及访问压力,所以配置上采取尽量节约数据库资源的方式 具体要求如下:初始化连接数为0连接不够,需要新创建时,每 ...
- 前端自动化神器gulp使用记录
1.安装压缩图片插件的时候,由于网络原因,死活安装不成功.由于imagemin本身就包含很多插件,安装的时候卡住了,很是郁闷.如果要压缩png图片,那就单独安装imagemin-pngquant压缩插 ...
- 选择语句----switch case
今天学习了选择语句的 switch case是多选一的情况可以使用. 案例: //分别输入月份 几号 输出是今年的多少天 //每年的1,3,5,7,8,10,12月是31天 //今年的2月是28天 其 ...
- org.json.JSONObject与com.google.gson.Gson
org.json库为JSON创始人编写的解析JSON的java库,Gson为Google为我们提供的解析JSON格式数据的库. Gson里最重要的对象有2个Gson 和GsonBuilder. Gso ...
- distinct用法
distinct可以列出不重复的记录,对于单个字段来说distinct使用比较简单,但是对于多个字段来说,distinct使用起来会使人发狂.而且貌似也没有见到微软对distinct使用多字段的任何说 ...
- iOS开原项目
http://www.lanrenios.com/ios/project/2013/0729/1433.html http://www.cnblogs.com/xiaobaizhu/archive/2 ...
- 转 Android中进入系统设置界面
Android软件时,常常需要打开系统设置或信息界面,来设置相关系统项或查看系统的相关信息,这时我们就可以使用以下语句来实现:(如打开“无线和网络设置”界面) Intent intent = new ...