1. 程式人生 > >spring整合redis(開啟事務)

spring整合redis(開啟事務)

前言

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;
}