Spring 事务原理总结三

news2024/9/24 11:23:38

今天这篇文章,我想梳理一下Spring事务用到的几个核心组件。这些核心组件是我们理解Spring事务原理的基础。通过它们我们可以体会学习一下Spring设计者设计Spring事务时的基本思路。这些组件是:TransactionInfo、TransactionStatus、TransactionManager、TransactionAttribute、TransactionAttributeSource、ReactiveTransactionSupport等等。

首先让我们一起看一下TransactionInfo类。它是一个位于TransactionAspectSupport类中的内部类(这个类是上一节介绍的TransactionInterceptor的父类,TransactionInterceptor是一个增强类,用于对那些需要事务的方法进行增强)。TransactionInfo类的源码如下:

protected static final class TransactionInfo {
    @Nullable
    private final PlatformTransactionManager transactionManager;
    @Nullable
    private final TransactionAttribute transactionAttribute;
    private final String joinpointIdentification;
    @Nullable
    private TransactionStatus transactionStatus;
    @Nullable
    private TransactionInfo oldTransactionInfo;
    public TransactionInfo(@Nullable PlatformTransactionManager transactionManager,
          @Nullable TransactionAttribute transactionAttribute, String joinpointIdentification) {
       this.transactionManager = transactionManager;
       this.transactionAttribute = transactionAttribute;
       this.joinpointIdentification = joinpointIdentification;
    }
    public PlatformTransactionManager getTransactionManager() {
       Assert.state(this.transactionManager != null, "No PlatformTransactionManager set");
       return this.transactionManager;
    }
    @Nullable
    public TransactionAttribute getTransactionAttribute() {
       return this.transactionAttribute;
    }
    /**
     * Return a String representation of this joinpoint (usually a Method call)
     * for use in logging.
     */
    public String getJoinpointIdentification() {
       return this.joinpointIdentification;
    }
    public void newTransactionStatus(@Nullable TransactionStatus status) {
       this.transactionStatus = status;
    }
    @Nullable
    public TransactionStatus getTransactionStatus() {
       return this.transactionStatus;
    }
    /**
     * Return whether a transaction was created by this aspect,
     * or whether we just have a placeholder to keep ThreadLocal stack integrity.
     */
    public boolean hasTransaction() {
       return (this.transactionStatus != null);
    }
    private void bindToThread() {
       // Expose current TransactionStatus, preserving any existing TransactionStatus
       // for restoration after this transaction is complete.
       this.oldTransactionInfo = transactionInfoHolder.get();
       transactionInfoHolder.set(this);
    }
    private void restoreThreadLocalStatus() {
       // Use stack to restore old transaction TransactionInfo.
       // Will be null if none was set.
       transactionInfoHolder.set(this.oldTransactionInfo);
    }
    @Override
    public String toString() {
       return (this.transactionAttribute != null ? this.transactionAttribute.toString() : "No transaction");
    }
}

通过源码不难看出TransactionInfo是一个最终类,也没有继承或实现其他接口或类,其中的属性有PlatformTransactionManager、TransactionAttribute、TransactionStatus、TransactionInfo(通过其变量名可以知道其代表的是老事务。还记得《Spring事务原理总结一》这篇文章中提到的事务传播属性吗?其中提到的REQUIRES_NEW可能就是通过这个属性来实现的)等。

然后让我们再一起看一下TransactionStatus接口,其又继承了SavepointManager、Flushable、TransactionExecution等接口。下面是该接口的源码:

public interface TransactionStatus extends TransactionExecution, SavepointManager, Flushable {

    /**
     * Return whether this transaction internally carries a savepoint,
     * that is, has been created as nested transaction based on a savepoint.
     * <p>This method is mainly here for diagnostic purposes, alongside
     * {@link #isNewTransaction()}. For programmatic handling of custom
     * savepoints, use the operations provided by {@link SavepointManager}.
     * <p>The default implementation returns {@code false}.
     * @see #isNewTransaction()
     * @see #createSavepoint()
     * @see #rollbackToSavepoint(Object)
     * @see #releaseSavepoint(Object)
     */
    default boolean hasSavepoint() {
       return false;
    }

    /**
     * Flush the underlying session to the datastore, if applicable:
     * for example, all affected Hibernate/JPA sessions.
     * <p>This is effectively just a hint and may be a no-op if the underlying
     * transaction manager does not have a flush concept. A flush signal may
     * get applied to the primary resource or to transaction synchronizations,
     * depending on the underlying resource.
     * <p>The default implementation is empty, considering flush as a no-op.
     */
    @Override
    default void flush() {
    }

}

下图则是TransactionStatus接口的继承体系:

接着来让我们来看一下TransactionAttribute接口,该位于org.springframework.transaction.interceptor包中,先来看一下其源码:

public interface TransactionAttribute extends TransactionDefinition {

    /**
     * Return a qualifier value associated with this transaction attribute.
     * <p>This may be used for choosing a corresponding transaction manager
     * to process this specific transaction.
     * @since 3.0
     */
    @Nullable
    String getQualifier();

    /**
     * Return labels associated with this transaction attribute.
     * <p>This may be used for applying specific transactional behavior
     * or follow a purely descriptive nature.
     * @since 5.3
     */
    Collection<String> getLabels();

    /**
     * Should we roll back on the given exception?
     * @param ex the exception to evaluate
     * @return whether to perform a rollback or not
     */
    boolean rollbackOn(Throwable ex);

}

下图是TransactionAttribute这个接口的继承体系(这里面我们中断关注RuleBasedTransactionAttribute这个类,第一次跟踪事务的执行代码时实际类型为RuleBasedTransactionAttribute):

最后再来看一下PlatformTransactionManager这个接口,该接口继承了TransactionManager接口,其有4个实现类,分别为AbstractPlatformTransactionManager、DataSourceTransactionManager、JdbcTransactionManager、JtaTransactionManager。个人理解它是一个管理接口。先看一下它的继承体系:

至此我们梳理了Spring事务涉及到的一些基本组件及其体系结构(这只是个人粗浅的认知,如果不全,还望各位大佬指正)。至于这些组件的作用,我们将在下一篇博客中逐一梳理。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1430470.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Chrono Engine学习总结】2-可视化

由于Chrono的官方教程在一些细节方面解释的并不清楚&#xff0c;自己做了一些尝试&#xff0c;做学习总结。 0、基本概念 类型说明&#xff1a; Chrono的可视化包括两块&#xff1a;实时可视化&#xff0c;以及离线/后处理可视化。 其中&#xff0c;实时可视化&#xff0c;又…

字节测试岗3面都过了,最后因为这个被刷。。。

&#x1f345; 视频学习&#xff1a;文末有免费的配套视频可观看 &#x1f345; 点击文末小卡片 &#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 说在前面 面试时最好不要虚报工资。本来字节跳动是很想去的&#xff0c;几轮面试也通过了&…

【Crypto | CTF】BUUCTF 大帝的密码武器1

天命&#xff1a;这题真的是来刷经验的&#xff0c;有点吐血 首先这题是贼简单&#xff0c;但我居然跪到了&#xff0c;所以特此写这一篇来惩戒自己心太大 拿到文件&#xff0c;文件写着zip&#xff0c;改成zip后缀名即可&#xff0c;也不算啥难的 打开里面的两份文件&#x…

PythonStudio 控件使用常用方式(七)TEdit

PythonStudio是一个极强的开发Python的IDE工具&#xff0c;它使用的是Delphi的控件&#xff0c;常用的内容是与Delphi一致的。但是相关文档并一定完整。现在我试试能否逐步把它的控件常用用法写一点点&#xff0c;也作为PythonStudio的参考。 TEdit就是单行文本框 常用属性 A…

移动Web——移动适配

移动适配 当屏幕宽度变化了&#xff0c;网页元素的宽高都要等比例变化&#xff0c;而且像间距、像素、字体等都是等比例变化的 移动适配方案 remvw 1、谷歌模拟器 2、屏幕分辨率 屏幕分辨率&#xff1a;纵横向上的像素点数&#xff0c;单位是px pc分辨率&#xff1a; 1920*10…

代码随想录算法训练营DAY11 | 栈与队列 (2)

一、LeetCode 20 有效的括号 题目链接&#xff1a;20.有效的括号https://leetcode.cn/problems/valid-parentheses/ 思路&#xff1a;遇到左括号直接进栈&#xff1b;遇到右括号判断站顶是否有匹配的括号&#xff0c;没有就返回flase&#xff0c;有就将栈顶元素出栈&#xff1…

2023年年终总结

又是一年到年底。趁着今天休息的空档&#xff0c;程序式的总结一下即将过去的2023年。 博客 先总结一下博客相关的数据吧。分为三块&#xff1a;CSDN博客、腾讯云开发者社区、微信公众平台。CSDN的数据从16年开始基本上都有统计&#xff0c;所以数据比较完整一些。公众号和腾讯…

《机器人SLAM导航核心技术与实战》第1季:第7章_SLAM中的数学基础

视频讲解 【第1季】7.第7章_SLAM中的数学基础-视频讲解 【第1季】7.1.第7章_SLAM中的数学基础_SLAM发展简史-视频讲解 【第1季】7.2.第7章_SLAM中的数学基础_SLAM中的概率理论-视频讲解 【第1季】7.3.第7章_SLAM中的数学基础_估计理论-视频讲解 【第1季】7.4.第7章_SLAM中的…

在Linux中对Nginx进行安全加固

准备工作 在IP为x.x.x.x的服务器上安装nginx&#xff0c;确保Linux系统为nginx环境。 检查nginx是否配置nginx账号锁定策略 配置nginx账号锁定策略&#xff0c;降低被攻击概率。 第一步&#xff0c;查看nginx的锁定状态。 命令&#xff1a;passwd -S nginx 若结果出现“P…

Java学习六、数组的定义与使用

一、数组的创建及初始化 数组是相同类型元素的集合&#xff0c;在内存中是一段连续的空间。 1.数组的创建 T[] 数组名 new T[N]; T&#xff1a;表示数组中存放元素的类型 N:表示数组长度 int[] array1 new int[10];//创建一个可以容纳10个int类型元素的数组 double[] array…

解决vue3+ts打包,ts类型检查报错导致打包失败,goview打包报错options

最近拉的开源大屏项目goview&#xff0c;在打包的过程中一直报Ts类型报错导致打包失败&#xff0c;项目的打包命令为&#xff1a; “build”: “vue-tsc --noEmit && vite build” 是因为 vue-tsc --noEmit 是 TypeScript 编译器&#xff08;tsc&#xff09;的命令&…

明道云入选亿欧智库《AIGC入局与低代码产品市场的发展研究》

2023年12月27日&#xff0c;亿欧智库正式发布**《AIGC入局与低代码产品市场的发展研究》**。该报告剖析了低代码/零代码市场的现状和发展趋势&#xff0c;深入探讨了大模型技术对此领域的影响和发展洞察。其中&#xff0c;亿欧智库将明道云作为标杆产品进行了研究和分析。 明…

【MySQL性能优化】- MySQL事务级别与锁机制

MySQL事务级别与锁机制 &#x1f604;生命不息&#xff0c;写作不止 &#x1f525; 继续踏上学习之路&#xff0c;学之分享笔记 &#x1f44a; 总有一天我也能像各位大佬一样 &#x1f3c6; 博客首页 怒放吧德德 To记录领地 &#x1f31d;分享学习心得&#xff0c;欢迎指正&…

[云顶数模]2024美赛CEF题成品参考论文+配套数据集+可执行代码+运行结果图

E题社区抗灾能力综合评估与决策模型研究 摘要&#xff1a;社区抗灾能力的提升对于灾害风险管理至关重要。本研究基于机器学 习方法&#xff0c;构建了社区抗灾能力预测模型&#xff0c;以评估社区在灾害事件中的表现。首先&#xff0c; 我们采用梯度提升树模型对社区基础设施、…

Enemy Rat(老鼠模型)

信息: - 模型有 1.491 个顶点。 - 纹理&#xff1a;颜色、法线、粗糙度、发射、金属、等级&#xff08;2048x2048 尺寸&#xff09; 下载&#xff1a; ​​Unity资源商店链接 资源下载链接 效果图&#xff1a;

NAS系统折腾记 – Emby搭建家庭多媒体服务器

Emby简介 Emby是一款优秀的媒体服务器软件&#xff0c;致力于为用户提供丰富的多媒体体验。通过Emby&#xff0c;您可以方便地在家庭内的各种设备上观看您喜爱的电影、电视剧和其他视频内容。而且&#xff0c;Emby还具备强大的媒体管理功能&#xff0c;让您的影视资源井然有序…

PSQL常用操作

目录 前言 准备工作 添加postgres用户 初始化数据库 启动服务 创建数据库 psql连接数据库 常规操作 数据库 schema相关 插件 其他 前言 老折腾&#xff0c;还是记录点啥吧...... 基于本地PG数据库(打包为绿色版本了)&#xff0c;实操记录&#xff0c;版本pgsql12…

大模型ReAct提示框架

Yao 等人于 2022 年引入了一个名为 ReAct 的框架&#xff0c;其中 LLM 用于以交错的方式生成推理轨迹&#xff08;reasoning traces&#xff09;和特定于任务的操作。 生成推理轨迹允许模型诱导、跟踪和更新行动计划&#xff0c;甚至处理异常。 操作步骤允许与外部源&#xff…

云尘 -- 铁三域控

描述&#xff1a; flag1 直接fscan开扫 发现存活两台机子123和141&#xff0c;其中141这台机子扫出来有ms17-010漏洞 继续信息收集&#xff0c;用nmap扫一波全端口&#xff0c;看看有没有遗漏 141这台机子一开始没扫到&#xff0c;看着提示使用-Pn再扫一遍就行了。因为如果当…

docker-学习-2

docker学习第二天 docker学习第二天1.docker和虚拟机的区别2.docker的底层隔离机制2.1 Namespaces(命名空间)2.1.1 什么是命名空间 2.2 Cgroups2.3 Union file systems2.4 Container format2.5 docker在底层如何做隔离的&#xff0c;如何进行资源限制的&#xff1f; 3. docker命…