spring整合redis(开启事务)

原文地址,转载请注明出处: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.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;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值