Spring事务框架之TransactionStatus源码解析

 更新时间:2023年08月29日 11:39:48   作者:福  
这篇文章主要为大家介绍了Spring事务框架之TransactionStatus源码示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

Spring事务框架

本文来分析TransactionStatus。

用来记录事务执行过程中的状态的,最终决定该事务能否提交、是否需要回滚等。

先来看一下TransactionStatus的类结构:

TransactionStatus

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}.
     * @see #isNewTransaction()
     * @see #createSavepoint()
     * @see #rollbackToSavepoint(Object)
     * @see #releaseSavepoint(Object)
     */
boolean hasSavepoint();
Flush the underlying session to the datastore, if applicable: for example, all affected Hibernate/JPA sessions.
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.
@Override
void flush();
}

他只定义了两个方法:

  • hasSavepoint:返回这个事务是否包含了savepoint,也就是说,是否基于嵌套事务创建了一个savepoint。savepoint的概念前面的文章已经分析过。
  • flush:这个应该是和Hibernate或JPA相关的,具体作用暂时不管了,不研究Hibernate相关的东西。

没了。

但是,这个接口继承了3个接口:TransactionExecution, SavepointManager, Flushable,我们简单看一眼:

TransactionExecution

这个接口很简单,是事务状态的一个通用接口,定义了当前事务是否是一个新事务的获取方法、设置当前事务为回滚状态、获取事务是否已经完成的方法等。

/**
 * Common representation of the current state of a transaction.
 * Serves as base interface for {@link TransactionStatus} as well as
 * {@link ReactiveTransaction}.
 *
 * @author Juergen Hoeller
 * @since 5.2
 */
public interface TransactionExecution {
    /**
     * Return whether the present transaction is new; otherwise participating
     * in an existing transaction, or potentially not running in an actual
     * transaction in the first place.
     */
    boolean isNewTransaction();
    /**
     * Set the transaction rollback-only. This instructs the transaction manager
     * that the only possible outcome of the transaction may be a rollback, as
     * alternative to throwing an exception which would in turn trigger a rollback.
     */
    void setRollbackOnly();
    /**
     * Return whether the transaction has been marked as rollback-only
     * (either by the application or by the transaction infrastructure).
     */
    boolean isRollbackOnly();
    /**
     * Return whether this transaction is completed, that is,
     * whether it has already been committed or rolled back.
     */
    boolean isCompleted();
}

SavepointManager

提供3个方法:创建保存点、回滚到保存点、释放保存点。

Object createSavepoint() throws TransactionException;
void rollbackToSavepoint(Object savepoint) throws TransactionException;
void releaseSavepoint(Object savepoint) throws TransactionException;

Flushable

不说了,就是上面的那个flush方法。

AbstactTransactionStatus & DefaultTransactionStatus

AbstactTransactionStatus持有事务的几个重要状态,业务执行后,Spring事务管理器需要通过状态判断事务是提交或者是回滚。

private boolean rollbackOnly = false;
    private boolean completed = false;
    @Nullable
    private Object savepoint;

Spring事务管理机制中TransactionStatus的最终落地实现是DefaultTransactionStatus,代码就不贴出了,比较简单。

其实我们通过对TransactioStatus的分析能够得出一个结论,那就是有savepoint的事务的回滚是通过TransactionStatus实现的。

TransactionStatus持有事务对象transaction,事务保存点savepoint是保存在transaction中,最终通过调用transaction的rollbackToSavepoint回滚事务到存储点。

好了,TransactionStatus就分析到这儿。

以上就是Spring事务框架之TransactionStatus的详细内容,更多关于Spring事务TransactionStatus的资料请关注脚本之家其它相关文章!

相关文章

  • SpringMvc微信支付回调示例代码

    SpringMvc微信支付回调示例代码

    微信一直是一个比较热门的词汇,今天这篇文章主要介绍的是SpringMvc微信支付回调的示例代码,对大家开发微信支付具有一定的参考借鉴价值,下面来一起看看吧。
    2016-09-09
  • 关于消息中间件RocketMQ的基本概念及功能

    关于消息中间件RocketMQ的基本概念及功能

    这篇文章主要介绍了关于消息中间件RocketMQ的基本概念及功能,RocketMQ作为一款纯java、分布式、队列模型的开源消息中间件,支持事务消息、顺序消息、批量消息、定时消息、消息回溯等,需要的朋友可以参考下
    2023-05-05
  • 使用SpringBoot进行身份验证和授权的示例详解

    使用SpringBoot进行身份验证和授权的示例详解

    在广阔的 Web 开发世界中,身份验证是每个数字领域的守护者,在本教程中,我们将了解如何以本机方式保护、验证和授权 Spring-Boot 应用程序的用户,并遵循框架的良好实践,希望对大家有所帮助
    2023-11-11
  • Java登录功能实现token生成与验证

    Java登录功能实现token生成与验证

    这篇文章介绍了Java登录功能实现token生成与验证,文中通过示例代码介绍的非常详细。对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-12-12
  • linux部署出现java文件操作报错:java.io.FileNotFoundException解决办法

    linux部署出现java文件操作报错:java.io.FileNotFoundException解决办法

    这篇文章主要g介绍了linux部署出现java文件操作报错:java.io.FileNotFoundException解决的相关资料,这个错误通常表示你的Spring Boot应用程序无法找到指定的文本文件,需要的朋友可以参考下
    2023-12-12
  • Java面试题冲刺第二十九天--JVM3

    Java面试题冲刺第二十九天--JVM3

    这篇文章主要为大家分享了最有价值的三道关于JVM的面试题,涵盖内容全面,包括数据结构和算法相关的题目、经典面试编程题等,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • JVM详解之汇编角度理解本地变量的生命周期

    JVM详解之汇编角度理解本地变量的生命周期

    这篇文章主要介绍了JVM详解之汇编角度理解本地变量的生命周期,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • SpringSecurity+Redis+Jwt实现用户认证授权

    SpringSecurity+Redis+Jwt实现用户认证授权

    SpringSecurity是一个强大且灵活的身份验证和访问控制框架,本文主要介绍了SpringSecurity+Redis+Jwt实现用户认证授权,具有一定的参考价值,感兴趣的可以了解一下
    2024-07-07
  • 全面理解java中的异常处理机制

    全面理解java中的异常处理机制

    下面小编就为大家带来一篇全面理解java中的异常处理机制。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • Spring Data JPA 注解Entity关联关系使用详解

    Spring Data JPA 注解Entity关联关系使用详解

    这篇文章主要为大家介绍了Spring Data JPA 注解Entity关联关系使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09

最新评论