事务的概述:逻辑上的一组操作,组成这组操作的各个单元,要么都成功,要么都失败。
事务应该加到业务层(service),如果遇到了特别复杂的业务逻辑,有可能会出现业务层之间的方法调用,从而造成事务嵌套(事务的传播行为主要是用来解决业务层方法相互调用的问题)。
保证多个操作在同一个事务中
保证多个操作不在同一事务中
嵌套事务
事务管理有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);
}
}
原文:https://www.cnblogs.com/fufengxi/p/14592711.html