天天看點

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. 送出事務

繼續閱讀