spring整合redis(开启事务)

来源:互联网 发布:centos 6.8 docker 编辑:程序博客网 时间:2024/05/21 22:53

原文地址,转载请注明出处:http://blog.csdn.net/qq_34021712/article/details/75949756   ©王赛超

前言

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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://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@Transactionalpublic 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;}


原创粉丝点击