Spring框架学习总结三
Spring的事务管理
事务的概述:逻辑上的一组操作,组成这组操作的各个单元,要么都成功,要么都失败。
Spring事务管理的API
- PlatformTransactionManager:平台事务管理器
- 平台事务管理器:接口是spring用于管理事务的真正对象。
- DataSourceTransactionManager:底层使用JDBC管理事务
- HibernateTransactionManager: 底层使用Hibernate管理事务
- 平台事务管理器:接口是spring用于管理事务的真正对象。
- TransactionDefinition:事务定义信息
- 事务定义: 用于定义事务的相关的信息、隔离级别、超时信息、传播行为(DML)、是否只读(查询)。
- TransactionStatus:事务的状态
- 事务状态:用于记录在事务管理过程中,事务的状态的对象。
- 事务管理的API的关系
- Spring进行事务管理的时候,首先平台事务管理器根据事务定义信息进行事务的管理,在事务管理过程中,产生各种状态,将这些状态的信息记录到事务状态的对象中。
Spring事务的传播行为
事务应该加到业务层(service),如果遇到了特别复杂的业务逻辑,有可能会出现业务层之间的方法调用,从而造成事务嵌套(事务的传播行为主要是用来解决业务层方法相互调用的问题)。
Spring 中提供了三类七种事务的传播行为
-
保证多个操作在同一个事务中
- PROPAGATION_REQUIRED:默认值(一般都只要用这个),如果A中有事务,使用A中的事务,如果A中没有事务,就创建新的事务,将操作包含进来。
- PROPAGATION_SUPPORTS:支持事务,如果A中有事务,使用A中的事务。如果A中没有事务,就不使用事务。
- PROPAGATION_MANDATORY:如果A有事务,就使用A中的事务,如果没有,就抛出异常。
-
保证多个操作不在同一事务中
- PROPAGATION_REQUIRES_NEW:如果A中有事务,将A的事务挂起(暂停),创建新的事务,只包含自身操作。如果A中没有事务,创建一个新事务,包含自身操作。
- PROPAGATION_NOT_SUPPORTED:如果A中有事务,就将A的事务挂起。并且不使用事务。
- PROPAGATION_NEVER:如果A中有事务,就会抛出异常。
-
嵌套事务
- PROPAGATION_NESTED: 嵌套事务,如果A中有事务,按照A的事务执行,执行完成后,设置一个保存点,执行B中的操作,如果没有异常,执行通过,如果有异常,可以选择回滚到最初始位置,也可以回滚到保存点。
搭建Spring的事务管理环境
事务管理有XML配置和注解配置两种,这里以转账的逻辑为例
方法一 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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 把要增强的目标交给Spring管理-->
<bean id="userServiceImpl" class="com.spring.service.impl.AccountServiceImpl"></bean>
<!--把通知所在的类也交给Spring管理-->
<bean id="myAspectj" class="com.spring.aspentj.MyAspectj"></bean>
<aop:config>
<aop:pointcut expression="execution(* com.spring.service.impl.AccountServiceImpl.transferAccount(..))" id="p1"/>
<!-- 配置切面 -->
<aop:aspect ref="myAspectj">
<aop:around method="checkMoney" pointcut-ref="p1"/>
</aop:aspect>
</aop:config>
<!-- 配置事物管理器的切面 -->
<bean id="transactionMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 那些类下的方法需要参与到当前的事物管理中 。配置切入点 -->
<aop:config>
<aop:advisor advice-ref="advice" pointcut="execution(* com.spring.service.impl.AccountServiceImpl.*(..))"/>
</aop:config>
<!-- 配置事物传播行为 :其实就是那些方法应该受什么样的事物控制-->
<tx:advice id="advice" transaction-manager="transactionMananger">
<tx:attributes>
<tx:method name="transferAccount" propagation="REQUIRED"/> <!--配置传播行为到指定的方法上-->
</tx:attributes>
</tx:advice>
</beans>
方法二 注解方式的声明式事务管理
同样是先引入AOP的开发包,然后恢复转账环境,配置事务管理器。
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
接下来就是在service层添加注解,在需要事务的类或或者方法上添加注解。而两者又有区别。定义到方法上,当前方法应用spring的声明式事务。定义到类上,当前类的所有方法都需要应用spring声明事务管理。
注解为@Transactional(propagation=Propagation.REQUIRED)
Service层
import com.spring.mapper.AccountMapper;
import com.spring.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
@Transactional(propagation = Propagation.REQUIRED) //在需要事务事务的方法上加这个注解
public void transferAccount(int out, int in, double money) {
//转出
accountMapper.tansferOut(out,money);
//转入
accountMapper.tansferInt(in,money);
}
}

浙公网安备 33010602011771号