天天看点

1. Spring事务实现概览1. Spring事务调用流程2. 事务实现示例3. 实现原理3. 事务实现流程源码分析

     Spring事务作为开发过程中最常用的框架之一,简化开发流程,提高开发效率,还将业务和事务实现进行解耦,使得开发者只需要关注业务的实现,而无需关注事务本身。

     本文主要介绍Spring的调用流程并demo简单概述事务的实现原理。

1. Spring事务调用流程

1. Spring事务实现概览1. Spring事务调用流程2. 事务实现示例3. 实现原理3. 事务实现流程源码分析

     该流程图描述了spring事务框架处理的整个调用过程,即调用者在调用事务方法的整个流程。主要步骤如下:

  1. 调用者调用Spring事务的方法,会被事务拦截器拦截并增强处理
  2. 拦截器中实现事务的创建,提交和回滚。
  3. 普通拦截器在事务拦截器之后进行增强处理
  4. 调用目标方法并逐层返回

2. 事务实现示例

     以下代码为通过@Transaction实现声明式事务:

/**
 *事务的service接口,采用jdk动态代理的方式(proxy-target-class=false,默认为false)必须要定义接口
 */
package x.y.service;

public interface FooService {
    Foo getFoo(String fooName);
    Foo getFoo(String fooName, String barName);
    void insertFoo(Foo foo);
    void updateFoo(Foo foo);
}

           
/**
 * 事务实现类
 */
@Transactional
public class DefaultFooService implements FooService {
    Foo getFoo(String fooName) {
        // ...
    }
    Foo getFoo(String fooName, String barName) {
        // ...
    }
    void insertFoo(Foo foo) {
        // ...
    }
    void updateFoo(Foo foo) {
        // ...
    }
}

           
<!-- from the file 'context.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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        https://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- this is the service object that we want to make transactional -->
    <bean id="fooService" class="x.y.service.DefaultFooService"/>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="txManager"/><!-- a PlatformTransactionManager is still required --> 

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- (this dependency is defined somewhere else) -->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- other <bean/> definitions here -->

</beans>

           

以上示例通过简单的配置就实现了DefaultFooService类的事务管理,开发者无需关心具体的事务实现逻辑,只需要通过@Transactional来声明使用事务即可,对于代码来说没有任何的侵入性。

3. 实现原理

从示例可以看到,与事务相关的配置只有两个:

  1. <tx:annotation-driven transaction-manager=“txManager”/>

    基于注解的事务行为配置,通过该配置实现对@Transactional注解的解析。

  2. @Transactional

    事务配置,配置具体是事务属性,例如事务的隔离级别,事务的传递性,超时时间等

    通过这两个简单的配置就能够实现DefaultFooService类中所有方法的事务控制,是不是很神奇!之后会以@Transactional为例对事务的实现源码进行分析。

Spring声明式事务具有以下优点:

  1. 简化开发:仅仅是通过简单的配置和@Transaction就能够实现事务功能
  2. 低耦合:事务的管理过程和业务代码进行隔离,两者不耦合
  3. 插拔式事务管理:通过PlatformTransationManager的抽象,不同的框架可以实现自定义的事务管理。示例中采用DataSourceTransactionManager。(Hibernate框架采用HibernateTransactionManager,还有JtaTransactionManager/JpaTransactionManager等)

3. 事务实现流程源码分析

 由于源码分析内容较多,根据以下内容进行拆分处理:

1. 自定义标签的解析

2. 注册InfrastructureAdvisorAutoProxyCreator

3. 获取对应的增强器

4. 事务增强器

1. 创建事务

2. 挂起事务

3. 回滚事务

4. 提交事务

继续阅读