• Spring中事务传播特性(Propagation)


    先来看一下Transactional源码,

    
    package org.springframework.transaction.annotation;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import org.springframework.core.annotation.AliasFor;
    
    @Target({ElementType.TYPE, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Inherited
    @Documented
    public @interface Transactional {
        @AliasFor("transactionManager")
        String value() default "";
    
        @AliasFor("value")
        String transactionManager() default "";
    
        Propagation propagation() default Propagation.REQUIRED;
    
        Isolation isolation() default Isolation.DEFAULT;
    
        int timeout() default -1;
    
        boolean readOnly() default false;
    
        Class<? extends Throwable>[] rollbackFor() default {};
    
        String[] rollbackForClassName() default {};
    
        Class<? extends Throwable>[] noRollbackFor() default {};
    
        String[] noRollbackForClassName() default {};
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    其中定义了事务的传播特性Propagation
    在这里插入图片描述

    接下来我们看一下Propagation源码

    /**
     * Enumeration that represents transaction propagation behaviors for use
     * with the {@link Transactional} annotation, corresponding to the
     * {@link TransactionDefinition} interface.
     *
     * @author Colin Sampaleanu
     * @author Juergen Hoeller
     * @since 1.2
     */
    public enum Propagation {
    
    	/**
    	 * Support a current transaction, create a new one if none exists.
    	 * Analogous to EJB transaction attribute of the same name.
    	 * 

    This is the default setting of a transaction annotation. */ REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED), /** * Support a current transaction, execute non-transactionally if none exists. * Analogous to EJB transaction attribute of the same name. *

    Note: For transaction managers with transaction synchronization, * {@code SUPPORTS} is slightly different from no transaction at all, * as it defines a transaction scope that synchronization will apply for. * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) * will be shared for the entire specified scope. Note that this depends on * the actual synchronization configuration of the transaction manager. * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization */ SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS), /** * Support a current transaction, throw an exception if none exists. * Analogous to EJB transaction attribute of the same name. */ MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY), /** * Create a new transaction, and suspend the current transaction if one exists. * Analogous to the EJB transaction attribute of the same name. *

    NOTE: Actual transaction suspension will not work out-of-the-box * on all transaction managers. This in particular applies to * {@link org.springframework.transaction.jta.JtaTransactionManager}, * which requires the {@code javax.transaction.TransactionManager} to be * made available to it (which is server-specific in standard Java EE). * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager */ REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW), /** * Execute non-transactionally, suspend the current transaction if one exists. * Analogous to EJB transaction attribute of the same name. *

    NOTE: Actual transaction suspension will not work out-of-the-box * on all transaction managers. This in particular applies to * {@link org.springframework.transaction.jta.JtaTransactionManager}, * which requires the {@code javax.transaction.TransactionManager} to be * made available to it (which is server-specific in standard Java EE). * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager */ NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED), /** * Execute non-transactionally, throw an exception if a transaction exists. * Analogous to EJB transaction attribute of the same name. */ NEVER(TransactionDefinition.PROPAGATION_NEVER), /** * Execute within a nested transaction if a current transaction exists, * behave like {@code REQUIRED} otherwise. There is no analogous feature in EJB. *

    Note: Actual creation of a nested transaction will only work on specific * transaction managers. Out of the box, this only applies to the JDBC * DataSourceTransactionManager. Some JTA providers might support nested * transactions as well. * @see org.springframework.jdbc.datasource.DataSourceTransactionManager */ NESTED(TransactionDefinition.PROPAGATION_NESTED); private final int value; Propagation(int value) { this.value = value; } public int value() { return this.value; } }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92

    Propagation是一个枚举类型,其中定了7种类型
    在这里插入图片描述

    名称特性
    REQUIRED使用当前的事务,如果当前没有事务,则自己新建一个事务,子方法必须运行在一个事务中;
    如果当前存在事务,则加入这个事务,成为一个整体。
    SUPPORTS如果当前有事务,则使用事务,如果当前没有事务,则不使用事务。
    MANDATORY该传播属性强制必须存在一个事务,如果不存在,则抛出异常。
    REQUIRES_NEW如果当前有事务,则挂起该事务,并且自己创建一个新的事务给自己使用;
    如果当前没有事务,则同 REQUIRED
    NOT_SUPPORTED如果当前有事务,则把事务挂起,自己不使用事务去运行数据库操作。
    NEVER不允许使用事务,如果当前有事务存在,则抛出异常
    NESTED如果当前有事务,则开启子事务(嵌套事务),嵌套事务是独立提交或者回滚;
    如果当前没有事务,则同REQUIRED
    如果主事务提交,则会携带子事务一起提交;
    如果主事务回滚,则子事务会一起回滚,相反,子事务异常,则父事务可以回滚,也可以不回滚。
  • 相关阅读:
    Python的基础语法(十)(持续更新)
    RuntimeError: “addmm_impl_cpu_“ not implemented for ‘Half‘
    Java编程练习题:面向对象练习
    java毕业设计大型商场应急预案管理系统mybatis+源码+调试部署+系统+数据库+lw
    Docker 第十三章 : Docker 三剑客之 Swarm(服务管理命令)
    2022.11.30 WAVE SUMMIT+ 深度学习开发者峰会
    【C++】C++入门
    洛谷P5731 【深基5.习6】蛇形方阵java版题解
    c语言从入门到实战——数组指针与函数指针
    Vue3.0 —— setup、ref、reactive和 computed的简介及使用
  • 原文地址:https://blog.csdn.net/qq_39361915/article/details/126674271