Spring事务管理之声明式事务

声明式事务实现方式一:基于TransactionProxyFactoryBean

TransactionFactoryBean用于简化声明式事务处理的代理工厂bean, 是一个方便的替代标准AOP ProxyFactoryBean与一个单独的TransactionInterceptor定义。

直接来看关于此方式的xml配置。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd      
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd    
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd        
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">   
    
    <context:property-placeholder location="classpath:jdbc.properties"/>
	<!-- 配置数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}"/>
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="0" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="20" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="0" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />
	</bean>
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 参考JDBC进行事务管理的时候首先需要connection.setAutoCommit(false);具体的事务提交应该拿到连接对象,
			故在此注入数据源的信息 -->
		<property name="dataSource" ref="dataSource"/>		
	</bean>
	<!-- 配置业务层的代理类 -->
	<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<!-- target:目标,即被代理的类 -->
		<property name="target" ref="accountService"/>
		<!-- transactionManager真正的事务管理器 -->
		<property name="transactionManager" ref="transactionManager"/>
		<!-- 配置被代理类的哪些方法需要被代理以及采用哪种传播行为,隔离级别,是否只读 -->
		<property name="transactionAttributes" >
		<props>
			<prop key="transfer">PROPAGATION_REQUIRED</prop>
		</props>
		</property>
	</bean>	
	<!-- 配置业务 -->
	<bean id="accountService" class="AccountServiceImpl">
		<property name="accountDao" ref="accountDao"/>
	</bean>	
	<!-- 配置数据库操作Dao层 -->
	<bean id="accountDao" class="AccountDaoImpl">
		<property name="dataSource" ref="dataSource"/>
	</bean>
</beans>  

关于TransactionProxyFactoryBean的属性说明

  • target:需要创建事务代理的目标对象,一般就是自己的业务类。
  • transactionManager:要使用的PlatformTransactionManager实现(例如,一个DataSourceTransactionManager的实例)。
  • transactionAttributes:每个目标方法名称(或方法名称模式)的事务属性(例如,传播行为和“只读”标记)。在此属性中可以指定被代理类的哪些方法利用<property>的<props>配置<props>中的key即是方法的名称。如下
 <prop key="*">PROPAGATION_REQUIRED</prop>表示匹配target中的所有方法。
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>表示匹配target中所有以find开头的方法。
<prop>格式:PROPAGATION,ISOLATION,readOnly,-Exception,+Exception
  • PROPAGATION_*:事务的传播行为
  • ISOLATION_* :事务的隔离级别
  • readOnly :是否只读
  • -Exception :发生哪些异常回滚
  • +Exception :发生哪些异常不会滚
demo中调用业务层的方法也需要用到代理类而不是原来的业务代码
public class SpringTest {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext2.xml");
		if(ac != null){
//			获取代理类
			AccountService accountService = (AccountService) ac.getBean("accountServiceProxy");
			accountService.transfer(1, 2, 100d);
		}
	}
}
声明式事务实现方式二:基于tx/aop的XML配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd      
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd    
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd        
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">   
    
    <context:property-placeholder location="classpath:jdbc.properties"/>
	<!-- 配置数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}"/>
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="0" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="20" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="0" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />
	</bean>
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 参考JDBC进行事务管理的时候首先需要connection.setAutoCommit(false);具体的事务提交应该拿到连接对象,
			故在此注入数据源的信息 -->
		<property name="dataSource" ref="dataSource"/>		
	</bean>
	
	<!-- 配置事务的通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager" >
		<tx:attributes>
			<tx:method name="transfer" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	<!-- 配置切面 -->
	<aop:config>
		<!-- 配置切入点 execution( )参数1为返回值类型,*代表任意返回值;第二个为类的全限定路径;+号代表子类;
		*代表子类的所有方法;..代表所有参数匹配 -->
		<aop:pointcut expression="execution(* AccountService+.*(..))" id="pointCut1"/>
		<!-- 配置切面 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut1"/>
	</aop:config>
	<!-- 配置业务 -->
	<bean id="accountService" class="AccountServiceImpl">
		<property name="accountDao" ref="accountDao"/>
	</bean>	
	<!-- 配置数据库操作Dao层 -->
	<bean id="accountDao" class="AccountDaoImpl">
		<property name="dataSource" ref="dataSource"/>
	</bean>
</beans>  
这种配置完成了自动代理,在使用的时候获得业务类本身也是一个代理类。
声明式事务实现方式三:基于注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    	http://www.springframework.org/schema/beans/spring-beans.xsd    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd      
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd    
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd        
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">   
    
    <context:property-placeholder location="classpath:jdbc.properties"/>
	<!-- 配置数据源 -->
	<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}"/>
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!-- 初始化连接大小 -->
		<property name="initialSize" value="0" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="20" />
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="20" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="0" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />
	</bean>
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 参考JDBC进行事务管理的时候首先需要connection.setAutoCommit(false);具体的事务提交应该拿到连接对象,
			故在此注入数据源的信息 -->
		<property name="dataSource" ref="dataSource"/>		
	</bean>
	
	<!-- 开启注解并配置事务管理器 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
	<!-- 配置业务 -->
	<bean id="accountService" class="AccountServiceImpl">
		<property name="accountDao" ref="accountDao"/>
	</bean>	
	<!-- 配置数据库操作Dao层 -->
	<bean id="accountDao" class="AccountDaoImpl">
		<property name="dataSource" ref="dataSource"/>
	</bean>
</beans>  
 使用时需要在需要事务管理的类或者方法上加入注解:
public class AccountServiceImpl implements AccountService {
	private AccountDao accountDao;
	
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor=Exception.class)
	public void transfer(final int in, final int out, final double money) {
				accountDao.rollOut(out, money);
				String temp = null;
				temp.toCharArray();
				accountDao.rollIn(in, money);
	}
}

 总结:
Spring将事务管理分成了两类:
  • 编程式事务
 手动编写代码进行事务管理(不推荐使用)。
  • 声明式事务
 基于TransactionProxyFactoryBean的方式(很少使用)需要为每一个类绑定一个代理类,维护不方便。
基于tx/aop的xml方式(经常使用)。
基于注解的方式(经常使用)开发代码最少。










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值