文章目录
1 编程式事务控制相关对象
1.1 PlatformTransactionManager
PlatformTransactionManager(平台事务管理器)接口是Spring的事务管理器,提供了常用的操作事务的方法:

PlatformTransactionManager是接口类型,但对于不同的Dao层技术有不同的实现类,如果Dao层技术是jdbc或mybatis时,它的实现类是DataSourceTransactionManager,如果Dao层技术是Hibernate时,它的实现类是HibernateTransactionManager
1.2 TransactionDefinition
TransactionDefinition是事务的定义信息对象,里面有如下方法:

1,事务的隔离级别:
设置隔离级别,可以解决事务并发产生的问题,如脏读,不可重复读和虚读
ISOLATION_DEFAULT: 默认
ISOLATION_READ_UNCOMMITTED: 读未提交
ISOLATION_READ_COMMITTED: 读已提交
ISOLATION_REPEATABLE_READ: 可重复读
ISOLATION_SERIALIZABLE: 串行化
2,事务的传播行为:
主要解决业务方法调用业务方法时之间事务统一性的问题

3,超时时间:默认是-1,没有超时限制,如果有,以秒为单位进行设置
是否只读:建议查询时只设置为只读
1.3 TransactionStatus
TransactionStatus接口提供事务具体的运行状态,方法如下:

2 声明式事务控制
Spring的声明式事务控制即通过配置替代编码来处理事务,声明式事务控制不侵入开发的组件,即业务逻辑对象不会意识到正处于事务管理之中,事实上也应该如此,事务管理是系统层面的服务,而部署业务逻辑的一部分,这样又进行了解耦,且维护修改起来也非常方便,Spring声明事务控制底层就是AOP
以一个转账的示例来看:

A向B转账500元,这其中需要发生两个操作,一个是从A的账户扣除500元,一个是给B的账户增加500元,这两个操作必须都成功,本次转账操作才能视为成功

转账的操作如果出现了异常,如上述示例已经从A的账户中扣除了钱,但此时程序异常,钱没有打到B的账户上,就造成了非常严重的问题,为此需要使用事务来控制,Spring的声明式事务控制不需要写代码,只用进行必要的配置即可,以AOP的视角配置即可
2.1 Spring的事务控制-XML
2.1.1 XML配置事务快速入门
1,在applicationContext.xml中引入tx命名空间:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
2,在applicationContext.xml中配置事务:
<!-- 配置平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 事务的AOP织入 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.coisini.service.*.*(..))"></aop:advisor>
</aop:config>
3,测试:

由于配置了事务管理,转账的操作也具备了原子性
2.1.2 事务属性参数的配置
<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 设置事务属性信息 -->
<tx:attributes>
<!-- 对任意方法进行事务增强 -->
<tx:method name="*" timeout="-1"/>
<!-- 对transfer进行事务增强 -->
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"></tx:method>
<!-- 对以update开头的方法进行事务增强 -->
<tx:method name="update*" read-only="false"></tx:method>
</tx:attributes>
</tx:advice>
2.2 Spring的事务控制-注解
1,将Dao层对象和Service层对象使用注解配置成Bean:

2,在applicationContext.xml中配置组件扫描:
<!-- 配置组件扫描 -->
<context:component-scan base-package="com.coisini"></context:component-scan>
3,对待进行事务增强的方法使用@Transactional注解进行配置:
@Service("accountService")
// 该类中所有方法都采用类上的事务增强,根据就近原则选择
@Transactional(isolation = Isolation.DEFAULT, timeout = -1)
public class AccountService implements IAccountService {
@Resource(name = "accountDao")
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
// 对该方法进行事务增强
@Transactional(isolation = Isolation.DEFAULT, timeout = -1)
public void transfer(String outMan, String inMan, double money) {
this.accountDao.out(outMan, money);
int i = 1 / 0;
this.accountDao.in(inMan, money);
}
}
4,在applicationContext.xml中配置事务的注解驱动:
<!-- 配置事务的注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
本文详细介绍了Spring的事务管理,包括编程式事务管理和声明式事务管理。编程式事务管理涉及PlatformTransactionManager、TransactionDefinition和TransactionStatus等核心对象。声明式事务管理则通过XML配置或注解实现,提供了更简洁的事务控制方式,降低了事务管理对业务代码的侵入性。文中还展示了如何在XML中配置事务增强,以及如何使用@Transactional注解进行事务控制。

被折叠的 条评论
为什么被折叠?



