自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

深入解析Spring事務原理,一文帶你全面理解

數(shù)據(jù)庫 MySQL
? 同一事務的多個實例在并發(fā)讀取數(shù)據(jù)時,會看到同樣的數(shù)據(jù)行(MySQL的默認事務隔離級別)。InnoDB和Falcon存儲引擎通過多版本并發(fā)控制(MVCC)機制解決了不可重復讀問題,存在幻讀問題。

前言

在Spring中,事務管理主要通過AOP功能實現(xiàn),對方法前后進行攔截,將事務處理的功能編織到攔截的方法中,Spring支持編程式事務管理和聲明式事務管理兩種方式。

  • 聲明式事務
  • @Transactional
  • 編程式事務
  • TransactionTemplate

  • TransactionManager

四大特性

  • 原子性(Atomicity):一個事務中的所有操作,要么都完成,要么都不執(zhí)行。對于一個事務來說,不可能只執(zhí)行其中的一部分。
  • 一致性(Consistency):數(shù)據(jù)庫總是從一個一致性的狀態(tài)轉(zhuǎn)換到另外一個一致性狀態(tài),事務前后數(shù)據(jù)的完整性必須保持一致。。
  • 隔離性(Isolation):一個事務所做的修改在最終提交以前,對其它事務是不可見的,多個事務之間的操作相互不影響。
  • 持久性(Durability):持久性是指一個事務一旦被提交,它對數(shù)據(jù)庫中數(shù)據(jù)的改變就是永久性的,接下來即使數(shù)據(jù)庫發(fā)生故障也不應該對其有任何影響。

隔離級別

  • Read Uncommitted(讀取未提交內(nèi)容):一個事務可以看到其他事務已執(zhí)行但是未提交的結(jié)果。本隔離級別很少用于實際應用,因為它的性能也不比其他級別好多少,并且存在臟讀問題。
  • Read Committed(讀取已提交內(nèi)容):一個事務只能看到其他事務已執(zhí)行并已提交的結(jié)果(Oracle、SQL Server默認隔離級別)。這種隔離級別支持不可重復讀,因為同一事務的其他實例在該實例處理期間可能會有新的commit,所以同一select可能返回不同結(jié)果。
  • Repeatable Read(可重讀):同一事務的多個實例在并發(fā)讀取數(shù)據(jù)時,會看到同樣的數(shù)據(jù)行(MySQL的默認事務隔離級別)。InnoDB和Falcon存儲引擎通過多版本并發(fā)控制(MVCC)機制解決了不可重復讀問題,存在幻讀問題。
  • Serializable(可串行化):最高的隔離級別,它通過強制事務排序,使之不可能相互沖突,從而解決幻讀問題。它是在每個讀的數(shù)據(jù)行上加上共享鎖。在這個級別,可能導致大量的超時現(xiàn)象和鎖競爭。

隔離級別

臟讀

不可重復讀

幻讀

Read Uncommitted



Read Committed

×



Repeatable Read

×

×


Serializable

×

×

×

傳播級別

傳播級別

含義

PROPAGATION_REQUIRED

支持當前事務,如果當前沒有事務,則新建一個事務

PROPAGATION_SUPPORTS

支持當前事務,如果當前沒有事務,則以非事務進行

PROPAGATION_MANDATORY

支持當前事務,如果當前沒有事務,則拋異常

PROPAGATION_REQUIRES_NEW

新建事務,如果當前存在事務,則把當前事務掛起

PROPAGATION_NESTED

如果當前存在事務,則在嵌套事務內(nèi)執(zhí)行。如果沒有,則進行與PROPAGATION_REQUIRED類似操作

PROPAGATION_NOT_SUPPORTED

以非事務進行,如果當前存在事務,則掛起事務,執(zhí)行當前邏輯,結(jié)束后恢復上下文的事務

PROPAGATION_NEVER

以非事務進行,如果當前存在事務,則拋異常

案例

導入相關(guān)依賴

數(shù)據(jù)源、數(shù)據(jù)庫驅(qū)動、spring-jdbc模塊

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.3.12.RELEASE</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

配置數(shù)據(jù)源

配置數(shù)據(jù)源、JdbcTemplate(Spring提供的簡化數(shù)據(jù)庫操作的工具)操作數(shù)據(jù)

@Bean
public DataSource dataSource(){
    DruidDataSource dataSource = new DruidDataSource();
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/scp");
    return dataSource;
}

@Bean
public JdbcTemplate jdbcTemplate(){
    //Spring對@Configuration類會特殊處理;給容器中加組件的方法,多次調(diào)用都只是從容器中找組件
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
    return jdbcTemplate;
}

數(shù)據(jù)訪問

@Repository
public class UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    
    @Transactional
    public void insert(){
        String sql = "INSERT INTO user (name,age) VALUES(?,?)";
        String username = UUID.randomUUID().toString().substring(0, 5);
        jdbcTemplate.update(sql, username,19);
        int a = 1/0;
    }

}

開啟事務,配置事務管理器

@EnableTransactionManagement  // 開啟事務
@ComponentScan("org.yian")
@Configuration
public class TxConfig {
    //數(shù)據(jù)源
    @Bean
    public DataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/scp");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(){
        //Spring對@Configuration類會特殊處理;給容器中加組件的方法,多次調(diào)用都只是從容器中找組件
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource());
        return jdbcTemplate;
    }

    //注冊事務管理器在容器中
    @Bean
    public PlatformTransactionManager transactionManager(){
        return new DataSourceTransactionManager(dataSource());
    }
}

測試類

@Test
public void test01(){
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TxConfig.class);

    UserService userService = applicationContext.getBean(UserService.class);

    userService.insertUser();
    applicationContext.close();
}

原理

Spring 事務管理的實現(xiàn)原理主要涉及兩個方面:事務管理器和代理機制:

  • 事務管理器(Transaction Manager):

Spring通過PlatformTransactionManager接口定義了事務管理器的標準。這個接口有多個實現(xiàn),包括常用的DataSourceTransactionManager、JpaTransactionManager、HibernateTransactionManager等,每個都專門用于不同的持久化技術(shù)。

事務管理器的主要職責是開始、提交或回滾事務。當使用聲明式事務管理時,開發(fā)者只需要配置相應的事務管理器,而不必親自編寫事務管理的代碼

  • 代理機制:
  • Spring 通過代理機制為事務管理提供支持。它使用AOP來在方法調(diào)用前后添加額外的邏輯,即切面。在事務管理中,這個額外的邏輯包括開啟、提交或回滾事務。

  • 當使用聲明式事務管理時,Spring 會動態(tài)創(chuàng)建一個代理對象,該代理對象包裝了目標對象(擁有業(yè)務邏輯的對象)。在方法調(diào)用時,代理對象會在執(zhí)行前后添加事務管理的邏輯

@EnableTransactionManagement:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({TransactionManagementConfigurationSelector.class})
public @interface EnableTransactionManagement {
    boolean proxyTargetClass() default false;

    AdviceMode mode() default AdviceMode.PROXY;

    int order() default Integer.MAX_VALUE;
}

TransactionManagementConfigurationSelector:

public class TransactionManagementConfigurationSelector extends AdviceModeImportSelector<EnableTransactionManagement> {
    public TransactionManagementConfigurationSelector() {
    }

    protected String[] selectImports(AdviceMode adviceMode) {
        switch (adviceMode) {
            case PROXY:
                return new String[]{AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()};
            case ASPECTJ:
                return new String[]{this.determineTransactionAspectClass()};
            default:
                return null;
        }
    }

    private String determineTransactionAspectClass() {
        return ClassUtils.isPresent("javax.transaction.Transactional", this.getClass().getClassLoader()) ? "org.springframework.transaction.aspectj.AspectJJtaTransactionManagementConfiguration" : "org.springframework.transaction.aspectj.AspectJTransactionManagementConfiguration";
    }
}

EnableTransactionManagement 會利用 TransactionManagementConfigurationSelector 給容器中會導入兩個組件 AutoProxyRegistrar、 ProxyTransactionManagementConfiguration

AutoProxyRegistrar:

public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean candidateFound = false;
        Set<String> annTypes = importingClassMetadata.getAnnotationTypes();
        Iterator var5 = annTypes.iterator();

        while(var5.hasNext()) {
            String annType = (String)var5.next();
            AnnotationAttributes candidate = AnnotationConfigUtils.attributesFor(importingClassMetadata, annType);
            if (candidate != null) {
                Object mode = candidate.get("mode");
                Object proxyTargetClass = candidate.get("proxyTargetClass");
                if (mode != null && proxyTargetClass != null && AdviceMode.class == mode.getClass() && Boolean.class == proxyTargetClass.getClass()) {
                    candidateFound = true;
                    if (mode == AdviceMode.PROXY) {
                        AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
                        if ((Boolean)proxyTargetClass) {
                            AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
                            return;
                        }
                    }
                }
            }
        }

AutoProxyRegistrar 給容器中注冊一個 InfrastructureAdvisorAutoProxyCreator 組件,利用后置處理器機制在對象創(chuàng)建以后,包裝對象,返回一個代理對象(增強器),代理對象執(zhí)行方法利用攔截器鏈進行調(diào)用

ProxyTransactionManagementConfiguration:

public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor(TransactionAttributeSource transactionAttributeSource, TransactionInterceptor transactionInterceptor) {
        BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
        advisor.setTransactionAttributeSource(transactionAttributeSource);
        advisor.setAdvice(transactionInterceptor);
        if (this.enableTx != null) {
            advisor.setOrder((Integer)this.enableTx.getNumber("order"));
        }

        return advisor;
    }

ProxyTransactionManagementConfiguration 給容器中注冊事務增強器

public TransactionAttributeSource transactionAttributeSource() {
        return new AnnotationTransactionAttributeSource();
    }

事務增強器要用事務注解的信息,AnnotationTransactionAttributeSource解析事務注解

public TransactionInterceptor transactionInterceptor(TransactionAttributeSource transactionAttributeSource) {
        TransactionInterceptor interceptor = new TransactionInterceptor();
        interceptor.setTransactionAttributeSource(transactionAttributeSource);
        if (this.txManager != null) {
            interceptor.setTransactionManager(this.txManager);
        }

        return interceptor;
    }

事務攔截器TransactionInterceptor保存了事務屬性信息,事務管理器,并且實現(xiàn)了 MethodInterceptor,在目標方法執(zhí)行的時候執(zhí)行攔截器鏈(事務攔截器)

TransactionAspectSupport:

protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass, InvocationCallback invocation) throws Throwable {
        TransactionAttributeSource tas = this.getTransactionAttributeSource();
        TransactionAttribute txAttr = tas != null ? tas.getTransactionAttribute(method, targetClass) : null;
        TransactionManager tm = this.determineTransactionManager(txAttr);
        Object retVal;
        if (this.reactiveAdapterRegistry != null && tm instanceof ReactiveTransactionManager) {
            boolean isSuspendingFunction = KotlinDetector.isSuspendingFunction(method);
            boolean hasSuspendingFlowReturnType = isSuspendingFunction && "kotlinx.coroutines.flow.Flow".equals((new MethodParameter(method, -1)).getParameterType().getName());
            ReactiveTransactionSupport txSupport = (ReactiveTransactionSupport)this.transactionSupportCache.computeIfAbsent(method, (key) -> {
                Class<?> reactiveType = isSuspendingFunction ? (hasSuspendingFlowReturnType ? Flux.class : Mono.class) : method.getReturnType();
                ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(reactiveType);
                if (adapter == null) {
                    throw new IllegalStateException("Cannot apply reactive transaction to non-reactive return type: " + method.getReturnType());
                } else {
                    return new ReactiveTransactionSupport(adapter);
                }
            });
            retVal = txSupport.invokeWithinTransaction(method, targetClass, invocation, txAttr, (ReactiveTransactionManager)tm);
            return isSuspendingFunction ? (hasSuspendingFlowReturnType ? TransactionAspectSupport.KotlinDelegate.asFlow((Publisher)retVal) : TransactionAspectSupport.KotlinDelegate.awaitSingleOrNull((Publisher)retVal, ((CoroutinesInvocationCallback)invocation).getContinuation())) : retVal;
        } else {
            PlatformTransactionManager ptm = this.asPlatformTransactionManager(tm);
            String joinpointIdentification = this.methodIdentification(method, targetClass, txAttr);
     
     .............................
     .............................
     .............................

  • 先獲取事務相關(guān)的屬性
  • 再獲取PlatformTransactionManager,如果事先沒有添加指定任何transactionmanger,最終會從容器中按照類型獲取一個PlatformTransactionManager
  • 執(zhí)行目標方法,如果異常,獲取到事務管理器,利用事務管理回滾操作;如果正常,利用事務管理器,提交事務
責任編輯:武曉燕 來源: 一安未來
相關(guān)推薦

2020-03-18 13:40:03

Spring事數(shù)據(jù)庫代碼

2019-10-11 08:41:35

JVM虛擬機語言

2022-05-11 07:38:45

SpringWebFlux

2023-07-17 10:45:03

向量數(shù)據(jù)庫NumPy

2021-09-08 17:42:45

JVM內(nèi)存模型

2021-03-10 10:55:51

SpringJava代碼

2024-05-22 09:45:49

2019-12-06 09:44:27

HTTP數(shù)據(jù)安全

2022-08-22 08:04:25

Spring事務Atomicity

2023-11-08 08:15:48

服務監(jiān)控Zipkin

2022-08-18 09:00:00

自動駕駛合成控制邁阿密

2022-07-25 11:10:09

PiniaVuexVue

2019-11-20 10:07:07

Redis數(shù)據(jù)系統(tǒng)

2022-08-03 08:01:16

CDN網(wǎng)站服務器

2023-04-04 08:01:47

2022-07-18 21:53:46

RocketMQ廣播消息

2023-02-06 18:10:00

前端

2021-01-27 11:10:49

JVM性能調(diào)優(yōu)

2023-11-06 08:16:19

APM系統(tǒng)運維

2021-05-29 10:11:00

Kafa數(shù)據(jù)業(yè)務
點贊
收藏

51CTO技術(shù)棧公眾號