JavaWeb笔记015 Spring中的事务

技术分析之Spring框架的事务管理相关的类和API

1. PlatformTransactionManager接口		-- 平台事务管理器.(真正管理事务的类)。该接口有具体的实现类,根据不同的持久层框架,需要选择不同的实现类!
2. TransactionDefinition接口			-- 事务定义信息.(事务的隔离级别,传播行为,超时,只读)
3. TransactionStatus接口				-- 事务的状态

4. 总结:上述对象之间的关系:平台事务管理器真正管理事务对象.根据事务定义的信息TransactionDefinition 进行事务管理,在管理事务中产生一些状态.将状态记录到TransactionStatus中

5. PlatformTransactionManager接口中实现类和常用的方法
	1. 接口的实现类
		* 如果使用的Spring的JDBC模板或者MyBatis框架,需要选择DataSourceTransactionManager实现类
		* 如果使用的是Hibernate的框架,需要选择HibernateTransactionManager实现类
	
	2. 该接口的常用方法
		* void commit(TransactionStatus status) 
		* TransactionStatus getTransaction(TransactionDefinition definition) 
		* void rollback(TransactionStatus status) 

6. TransactionDefinition
	1. 事务隔离级别的常量
		* static int ISOLATION_DEFAULT 					-- 采用数据库的默认隔离级别
		* static int ISOLATION_READ_UNCOMMITTED 
		* static int ISOLATION_READ_COMMITTED 
		* static int ISOLATION_REPEATABLE_READ 
		* static int ISOLATION_SERIALIZABLE 
	
	2. 事务的传播行为常量(不用设置,使用默认值)
		* 先解释什么是事务的传播行为:解决的是业务层之间的方法调用!!
		
		* PROPAGATION_REQUIRED(默认值)	-- A中有事务,使用A中的事务.如果没有,B就会开启一个新的事务,将A包含进来.(保证A,B在同一个事务中),默认值!!
		* PROPAGATION_SUPPORTS			-- A中有事务,使用A中的事务.如果A中没有事务.那么B也不使用事务.
		* PROPAGATION_MANDATORY			-- A中有事务,使用A中的事务.如果A没有事务.抛出异常.
		
		* PROPAGATION_REQUIRES_NEW(记)-- A中有事务,将A中的事务挂起.B创建一个新的事务.(保证A,B没有在一个事务中)
		* PROPAGATION_NOT_SUPPORTED		-- A中有事务,将A中的事务挂起.
		* PROPAGATION_NEVER 			-- A中有事务,抛出异常.
		
		* PROPAGATION_NESTED(记)		-- 嵌套事务.当A执行之后,就会在这个位置设置一个保存点.如果B没有问题.执行通过.如果B出现异常,运行客户根据需求回滚(选择回滚到保存点或者是最初始状态)

技术分析之Spring框架的事务管理的分类

1. Spring的事务管理的分类
	1. Spring的编程式事务管理(不推荐使用)
		* 通过手动编写代码的方式完成事务的管理(不推荐)
	
	2. Spring的声明式事务管理(底层采用AOP的技术)
		* 通过一段配置的方式完成事务的管理(重点掌握注解的方式)

技术分析之Spring框架的事务管理之编程式的事务管理(了解)

1. 说明:Spring为了简化事务管理的代码:提供了模板类 TransactionTemplate,所以手动编程的方式来管理事务,只需要使用该模板类即可!!

2. 手动编程方式的具体步骤如下:
	1. 步骤一:配置一个事务管理器,Spring使用PlatformTransactionManager接口来管理事务,所以咱们需要使用到他的实现类!!
		<!-- 配置事务管理器 -->
		<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name="dataSource" ref="dataSource"/>
		</bean>
	
	2. 步骤二:配置事务管理的模板
		<!-- 配置事务管理的模板 -->
		<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
			<property name="transactionManager" ref="transactionManager"/>
		</bean>
	
	3. 步骤三:在需要进行事务管理的类中,注入事务管理的模板.
		<bean id="accountService" class="com.demo1.AccountServiceImpl">
			<property name="accountDao" ref="accountDao"/>
			<property name="transactionTemplate" ref="transactionTemplate"/>
		</bean>
	
	4. 步骤四:在业务层使用模板管理事务:
		// 注入事务模板对象
		private TransactionTemplate transactionTemplate;
		public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
			this.transactionTemplate = transactionTemplate;
		}
		
		public void pay(final String out, final String in, final double money) {
			transactionTemplate.execute(new TransactionCallbackWithoutResult() {
				
				protected void doInTransactionWithoutResult(TransactionStatus status) {
					// 扣钱
					accountDao.outMoney(out, money);
					int a = 10/0;
					// 加钱
					accountDao.inMoney(in, money);
				}
			});
		}

Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)

1. 配置事务管理器
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

2. 配置事务增强(增强规则)
	<!-- 配置事务增强,通知部分(属于AOP中的通知角色) -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!--
				name		:绑定事务的方法名,可以使用通配符,可以配置多个。
				propagation	:传播行为
				isolation	:隔离级别
				read-only	:是否只读
				timeout		:超时信息
				rollback-for:发生哪些异常回滚.
				no-rollback-for:发生哪些异常不回滚.
			 -->
			<!-- 哪些方法加事务,可以使用通配符*,例如:pay* -->
			<tx:method name="pay" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
    <!--aop方式,必须加上-->
    <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>



3. 配置AOP的切面(即哪些方法增强):切入点和通知结合
	<!-- 配置AOP切面产生代理 -->
	<aop:config>
    	<aop:advisor advice-ref="myAdvice" pointcut="execution(* com.demo2.AccountServiceImpl.pay(..))"/>
    </aop:config>
	
	* 注意:如果是自己编写的切面,使用<aop:aspect>标签,如果是系统制作的,使用<aop:advisor>标签。

4. 编写测试类
	@RunWith(SpringJUnit4ClassRunner.class)
	@ContextConfiguration("classpath:applicationContext2.xml")
	public class Demo2 {
		
		@Resource(name="accountService")
		private AccountService accountService;
		
		@Test
		public void run1(){
			accountService.pay("冠希", "美美", 1000);
		}
	}

Spring框架的事务管理之基于AspectJ的注解方式(重点掌握,最简单的方式)

1. 配置事务管理器
	<!-- 配置事务管理器  -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

2. 开启注解事务
	<!-- 开启注解事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

3. 在业务层上添加一个注解:@Transactional,类或者方法上加都可以

4. 编写测试类
	@RunWith(SpringJUnit4ClassRunner.class)
	@ContextConfiguration("classpath:applicationContext3.xml")
	public class Demo3 {
		
		@Resource(name="accountService")
		private AccountService accountService;
		
		@Test
		public void run1(){
			accountService.pay("冠希", "美美", 1000);
		}
	}
    

    @Transactional
    public class AccountServiceImpl implements AccountService{
        ...
    }

手动回滚事务
如果在注解事物中需要回滚事物怎么办?

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章