当前位置: 首页 > news >正文

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 {};
}

其中定义了事务的传播特性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.
	 * <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;
	}

}

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

名称特性
REQUIRED使用当前的事务,如果当前没有事务,则自己新建一个事务,子方法必须运行在一个事务中;
如果当前存在事务,则加入这个事务,成为一个整体。
SUPPORTS如果当前有事务,则使用事务,如果当前没有事务,则不使用事务。
MANDATORY该传播属性强制必须存在一个事务,如果不存在,则抛出异常。
REQUIRES_NEW如果当前有事务,则挂起该事务,并且自己创建一个新的事务给自己使用;
如果当前没有事务,则同 REQUIRED
NOT_SUPPORTED如果当前有事务,则把事务挂起,自己不使用事务去运行数据库操作。
NEVER不允许使用事务,如果当前有事务存在,则抛出异常
NESTED如果当前有事务,则开启子事务(嵌套事务),嵌套事务是独立提交或者回滚;
如果当前没有事务,则同REQUIRED
如果主事务提交,则会携带子事务一起提交;
如果主事务回滚,则子事务会一起回滚,相反,子事务异常,则父事务可以回滚,也可以不回滚。

相关文章:

  • Matlab:Matlab编程语言应用之数学统计(柱状图、曲线分析等)的使用方法简介、案例实现之详细攻略
  • YOLOv7改进之二十五:引入Swin Transformer
  • 03 nginx 是如何自动推导文件的 content-type 的
  • Java 8 Stream 从入门到进阶——像SQL一样玩转集合
  • C++STL详解(五)mapset的使用及其模拟实现
  • 串口通信-USART和UART的区别
  • Docker常见操作
  • YOLOV7详细解读(一)网络架构解读
  • 【侯捷C++-----STL与泛型编程】
  • es6对象的扩展、对象的新增方法
  • Stream流
  • DRM系列(8)之prepare_signaling
  • 企业级C++项目那些事(1):VSQt相关设置
  • 基于51单片机温湿度控制器proteus仿真设计
  • adb常用命令总结[持续更新]
  • [译]前端离线指南(上)
  • 【翻译】babel对TC39装饰器草案的实现
  • 【跃迁之路】【699天】程序员高效学习方法论探索系列(实验阶段456-2019.1.19)...
  • AHK 中 = 和 == 等比较运算符的用法
  • Apache Spark Streaming 使用实例
  • Docker 笔记(2):Dockerfile
  • Facebook AccountKit 接入的坑点
  • iOS 系统授权开发
  • IOS评论框不贴底(ios12新bug)
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • Javascript Math对象和Date对象常用方法详解
  • k8s如何管理Pod
  • OSS Web直传 (文件图片)
  • Redis中的lru算法实现
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 纯 javascript 半自动式下滑一定高度,导航栏固定
  • 类orAPI - 收藏集 - 掘金
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 温故知新之javascript面向对象
  • 如何用纯 CSS 创作一个菱形 loader 动画
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (搬运以学习)flask 上下文的实现
  • (二)Linux——Linux常用指令
  • (论文阅读11/100)Fast R-CNN
  • (转)菜鸟学数据库(三)——存储过程
  • (转)清华学霸演讲稿:永远不要说你已经尽力了
  • .FileZilla的使用和主动模式被动模式介绍
  • .NET 将混合了多个不同平台(Windows Mac Linux)的文件 目录的路径格式化成同一个平台下的路径
  • .Net 路由处理厉害了
  • .Net的DataSet直接与SQL2005交互
  • .net下简单快捷的数值高低位切换
  • .vue文件怎么使用_vue调试工具vue-devtools的安装
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • @test注解_Spring 自定义注解你了解过吗?
  • [1159]adb判断手机屏幕状态并点亮屏幕
  • [20170705]diff比较执行结果的内容.txt