Spring的事务管理
什么事务
事务:逻辑上的一组操作,组成这组操作的各个单元,要么全都成功,要么全都失败。
Spring的事务管理的API
PlatformTransactionManager:平台事务管理器
平台事务管理器:接口,是Spring用于管理事务的真正的对象。
DataSourceTransactionManager :底层使用JDBC管理事务
HibernateTransactionManager :底层使用Hibernate管理事务
TransactionDefinition :事务定义信息
事务定义:用于定义事务的相关的信息,隔离级别、超时信息、传播行为(DML)、是否只读(查询)
TransactionStatus:事务的状态
事务状态:用于记录在事务管理过程中,事务的状态的对象。
事务管理的API的关系:
Spring进行事务管理的时候,首先平台事务管理器根据事务定义信息进行事务的管理,在事务管理过程中,产生各种状态,将这些状态的信息记录到事务状态的对象中。
Spring的事务的传播行为
Spring的传播行为
Spring中提供了七种事务的传播行为:
保证多个操作在同一个事务中:
PROPAGATION_REQUIRED :默认值(一般都只要用这个),如果A中有事务,使用A中的事务,如果A没有,创建一个新的事务,将操作包含进来
PROPAGATION_SUPPORTS :支持事务,如果A中有事务,使用A中的事务。如果A没有事务,不使用事务。
PROPAGATION_MANDATORY :如果A中有事务,使用A中的事务。如果A没有事务,抛出异常。
保证多个操作不在同一个事务中
PROPAGATION_REQUIRES_NEW :如果A中有事务,将A的事务挂起(暂停),创建新事务,只包含自身操作。如果A中没有事务,创建一个新事务,包含自身操作。
PROPAGATION_NOT_SUPPORTED :如果A中有事务,将A的事务挂起。不使用事务管理。
PROPAGATION_NEVER :如果A中有事务,报异常。
嵌套式事务:
PROPAGATION_NESTED :嵌套事务,如果A中有事务,按照A的事务执行,执行完成后,设置一个保存点,执行B中的操作,如果没有异常,执行通过,如果有异常,可以选择回滚到最初始位置,也可以回滚到保存点。
Spring的事务管理
Spring的事务管理:声明式事务管理(通过配置实现)—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: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">
配置事物管理器的切面
<bean id="transactionMananger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
配置事物传播行为 :其实就是那些方法应该受什么样的事物控制
<tx:advice id="advice" transaction-manager="transactionMananger">
<tx:attributes>
<tx:method name="transferAccount" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
那些类下的方法需要参与到当前的事物管理中 ,配置切点
<aop:config>
<aop:advisor advice-ref="advice" pointcut="execution(* com.sst.service.AccountServiceImpl.*(..))"/>
</aop:config>
<?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"/>
第四步:在sevice层添加注解
在需要事务的类或方法上添加注解:
定义到方法上:当前方法应用spring的声明式事务
定义到类上: 当前类的所有的方法都应用Spring声明式事务管理;
@Transactional(propagation=Propagation.REQUIRED)
package com.spring.service.impl;
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);
int a = 10/0;//这个地方会报异常
//转入
accountMapper.tansferInt(in,money);
}
}
``
```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>
<!--开启注解的事务管理:指定平台事务管理器-->
<tx:annotation-driven transaction-manager="transactionMananger"></tx:annotation-driven>
</beans>