欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

详解事务的传播

程序员文章站 2022-07-14 23:02:39
...

详解事务的传播

@Transactional(propagation = Propagation.SUPPORTS)

事务种类(Propagation)

  /**
     * 事务传播 - Propagation
     *      REQUIRED: 使用当前的事务,如果当前没有事务,则自己新建一个事务,子方法是必须运行在一个事务中的;
     *                如果当前存在事务,则加入这个事务,成为一个整体。
     *                举例:领导没饭吃,我有钱,我会自己买了自己吃;领导有的吃,会分给你一起吃。
     *      SUPPORTS: 如果当前有事务,则使用事务;如果当前没有事务,则不使用事务。
     *                举例:领导没饭吃,我也没饭吃;领导有饭吃,我也有饭吃。
     *      MANDATORY: 该传播属性强制必须存在一个事务,如果不存在,则抛出异常
     *                 举例:领导必须管饭,不管饭没饭吃,我就不乐意了,就不干了(抛出异常)
     *      REQUIRES_NEW: 如果当前有事务,则挂起该事务,并且自己创建一个新的事务给自己使用;
     *                    如果当前没有事务,则同 REQUIRED
     *                    举例:领导有饭吃,我偏不要,我自己买了自己吃
     *      NOT_SUPPORTED: 如果当前有事务,则把事务挂起,自己不适用事务去运行数据库操作
     *                     举例:领导有饭吃,分一点给你,我太忙了,放一边,我不吃
     *      NEVER: 如果当前有事务存在,则抛出异常
     *             举例:领导有饭给你吃,我不想吃,我热爱工作,我抛出异常
     *      NESTED: 如果当前有事务,则开启子事务(嵌套事务),嵌套事务是独立提交或者回滚;
     *              如果当前没有事务,则同 REQUIRED。
     *              但是如果主事务提交,则会携带子事务一起提交。
     *              如果主事务回滚,则子事务会一起回滚。相反,子事务异常,则父事务可以回滚或不回滚。
     *              举例:领导决策不对,老板怪罪,领导带着小弟一同受罪。小弟出了差错,领导可以推卸责任。
     */


/**
 * 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.
	 * <p>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.
	 * <p>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.
	 * <p><b>NOTE:</b> 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.
	 * <p><b>NOTE:</b> 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.
	 * <p>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;
	}

}

api 引入test

详解事务的传播

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

详解事务的传播

package com.test;

import com.imooc.Application;
import com.imooc.service.StuService;
import com.imooc.service.TestTransService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = Application.class)
public class TransTest {

    @Autowired
    private StuService stuService;

    @Autowired
    private TestTransService testTransService;

//    @Test
    public void myTest() {
//        stuService.testPropagationTrans();
        testTransService.testPropagationTrans();
    }

}

事务传播

@Transactional(propagation = Propagation.REQUIRED)

1.父方法有事务,子方法无,此时子方法出现异常,子父都会回滚

2.父方法无事务,子方法有事务,父方法无异常不会回滚,子方法出现异常会回滚

@Transactional(propagation = Propagation.SUPPORTS)

如果当前有事务,则使用事务;如果当前没有事务,则不使用事务,可用于查询

1.父方法无事务,子方法有事务,子方法异常之前不会回滚

@Transactional(propagation = Propagation.MANDATORY)

详解事务的传播

当前必须有事务,无事务则报错

@Transactional(propagation = Propagation.REQUIRES_NEW)

如果当前有事务,会开启一个事务,分别去提交

1.父方法无事务,子方法有事务,父子是两个事务,子方法会回滚

2.父方法有事务(required),子方法有事务,异常在字方法里面,父子均会回滚;异常如果只在父方法里面,只有父方法会回滚

详解事务的传播

@Transactional(propagation = Propagation.NOT_SUPPORTED)

此方法不使用事务

父方法存在事务(required)的话,子方法使用该事务级别,子方法出现异常不会回滚,父方法会回滚

@Transactional(propagation = Propagation.NEVER)

存在事务,报异常

详解事务的传播

@Transactional(propagation = Propagation.NESTED)

父子事务一起去提交

必须当前存在一个事务,才会开启一个子事务

父方法出现异常,子方法无异常,也就是父事务出现异常,父子事务均会回滚

详解事务的传播

添加try catch后,父事务和其他事物不受影响

详解事务的传播

@EnableTransactionManagement

可以直接使用@Transactional,启动类里面无需加上@EnableTransactionManagement注解

已经在spring.factories里面自动装配

spring.factories

详解事务的传播

详解事务的传播

相关标签: 架构师一 spring