spring整合redis(开启事务)
2017-07-24 00:58
351 查看
原文地址,转载请注明出处:http://blog.csdn.net/qq_34021712/article/details/75949756
©王赛超
上一篇博客中 已经整合了redis,只需要修改两处就可以了,没有整合的参考博客:http://blog.csdn.net/qq_34021712/article/details/75949706
©王赛超
前言
redis整合spring之后 如果需要用到事务,要开启redis的事务管理,redis使用命令multi来开启一个事务,使用命令exec 来提交一个事务,redis和spring整合之后,使用的是 redisTemplate 来操作使用redis的,使用redisTemplate开启事务之后,spring会帮我们拿到了事务中绑定的连接,使用这个相同的链接,多次操作redis是可以异常的回滚的,前提是要开启redis事务,并开启spring事务注解方式,redisTemplate开启事务非常简单,本人亲测有效,下面直接开始教程:上一篇博客中 已经整合了redis,只需要修改两处就可以了,没有整合的参考博客:http://blog.csdn.net/qq_34021712/article/details/75949706
第一处:修改applicationContext-redis.xml
<!--在redisTemplate模板下 添加属性enableTransactionSupport 为true(开启事务) 默认是false --> <!--redis操作模版,使用该对象可以操作redis --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" > <property name="connectionFactory" ref="jedisConnectionFactory" /> <!--如果不配置Serializer,那么存储的时候缺省使用String,如果用User类型存储,那么会提示错误User can't cast to String!! --> <property name="keySerializer" > <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer" > <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/> </property> <!--开启事务 --> <property name="enableTransactionSupport" value="true"></property> </bean >
第二处:修改applicationContext-service.xml
注意:之前使用的是aop事务,测试aop事务对redis无效,修改为基于注解形式的spring事务<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"> <!-- 扫描包加载Service实现类 --> <context:component-scan base-package="com.*.*.service"></context:component-scan> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
测试
在service层 方法上标注@Transactional 并在方法最后一行抛了一个异常,测试redis事务是生效的。@Override @Transactional public void testTransaction() { redisUtil.set("a6a6", "a6a6"); User_Risk user_Risk=new User_Risk(); user_Risk.setScore("58"); user_Risk.setUserID("15532002777"); user_Risk.setLabelcode("666"); user_Risk.setUpdateTime(new Date()); verifyUserRiskMapper.insertIntoTb_user_risk(user_Risk); int a=10/0; }
相关文章推荐
- 09-MyBatis_与spring的整合之添加事务
- spring + springMVC +mybatis+ maven +redis + mysql 框架整合
- Spring Boot+Shiro+Redis(redisson)整合时,采用内嵌tomcat启动错误原因分析
- spring与hibernate整合事务管理的理解
- spring整合redis时 因为版本问题导致bean注入不进去问题
- spring与mybatis整合的事务问题记录
- Spring整合JMS(四)——事务管理
- Spring+Mybatis整合事务不起作用之…
- spring下的Redis事务和管道
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【二】【整合springSecurity】
- redis整合spring mybatis -- 缓存方案
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo
- spring 整合redis
- 85-002-3 spring与hibernate的整合(采用AOP来管理事务实现声明式事务)
- Spring整合Redis-配置文件
- Redis 整合spring ,做mysql的缓存
- Java中Jedis操作Redis与Spring的整合
- Redis整合Spring综合使用缓存实例
- spring整合hibernate事务管理的四种方式,以及事务的传播行为和隔离级别介绍
- SpringBoot 整合 Redis 的简单案例