SSM-Spring-5-事务

本文详细介绍了Spring的事务管理,包括编程式事务管理和声明式事务管理。编程式事务管理涉及PlatformTransactionManager、TransactionDefinition和TransactionStatus等核心对象。声明式事务管理则通过XML配置或注解实现,提供了更简洁的事务控制方式,降低了事务管理对业务代码的侵入性。文中还展示了如何在XML中配置事务增强,以及如何使用@Transactional注解进行事务控制。
摘要由CSDN通过智能技术生成

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值