spring的TransactionSynchronizationAdapter事务源码解析

 更新时间:2023年09月11日 11:31:52   作者:codecraft  
这篇文章主要介绍了spring的TransactionSynchronizationAdapter事务源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下spring的TransactionSynchronizationAdapter

示例代码

public void insert(TechBook techBook){
        bookMapper.insert(techBook);
       // send after tx commit but is async
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
            @Override
            public void afterCommit() {
                System.out.println("send email after transaction commit...");
            }
        }
       );
        System.out.println("service end");
    }
使用TransactionSynchronizationManager.registerSynchronization注册了一个TransactionSynchronizationAdapter,在其afterCommit方法也就是事务提交成功之后执行一些额外逻辑

TransactionSynchronizationAdapter

org/springframework/transaction/support/TransactionSynchronizationAdapter.java

public abstract class TransactionSynchronizationAdapter implements TransactionSynchronization, Ordered {
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
    @Override
    public void suspend() {
    }
    @Override
    public void resume() {
    }
    @Override
    public void flush() {
    }
    @Override
    public void beforeCommit(boolean readOnly) {
    }
    @Override
    public void beforeCompletion() {
    }
    @Override
    public void afterCommit() {
    }
    @Override
    public void afterCompletion(int status) {
    }
}
TransactionSynchronizationAdapter是个抽象类,声明实现TransactionSynchronization及Ordered接口

TransactionSynchronization

org/springframework/transaction/support/TransactionSynchronization.java

/**
     * Invoked after transaction commit. Can perform further operations right
     * <i>after</i> the main transaction has <i>successfully</i> committed.
     * <p>Can e.g. commit further operations that are supposed to follow on a successful
     * commit of the main transaction, like confirmation messages or emails.
     * <p><b>NOTE:</b> The transaction will have been committed already, but the
     * transactional resources might still be active and accessible. As a consequence,
     * any data access code triggered at this point will still "participate" in the
     * original transaction, allowing to perform some cleanup (with no commit following
     * anymore!), unless it explicitly declares that it needs to run in a separate
     * transaction. Hence: <b>Use {@code PROPAGATION_REQUIRES_NEW} for any
     * transactional operation that is called from here.</b>
     * @throws RuntimeException in case of errors; will be <b>propagated to the caller</b>
     * (note: do not throw TransactionException subclasses here!)
     */
    default void afterCommit() {
    }

注意这里注释说明了异常不会被捕获,而且建议不在这里抛出TransactionException的子类;另外对于afterCommit有数据库相关操作的建议使用PROPAGATION_REQUIRES_NEW这个事务传播级别,不然afterCommit的操作可能不会生效

registerSynchronization

org/springframework/transaction/support/TransactionSynchronizationManager.java

private static final ThreadLocal<Set<TransactionSynchronization>> synchronizations =
            new NamedThreadLocal<>("Transaction synchronizations");
    /**
     * Register a new transaction synchronization for the current thread.
     * Typically called by resource management code.
     * <p>Note that synchronizations can implement the
     * {@link org.springframework.core.Ordered} interface.
     * They will be executed in an order according to their order value (if any).
     * @param synchronization the synchronization object to register
     * @throws IllegalStateException if transaction synchronization is not active
     * @see org.springframework.core.Ordered
     */
    public static void registerSynchronization(TransactionSynchronization synchronization)
            throws IllegalStateException {
        Assert.notNull(synchronization, "TransactionSynchronization must not be null");
        Set<TransactionSynchronization> synchs = synchronizations.get();
        if (synchs == null) {
            throw new IllegalStateException("Transaction synchronization is not active");
        }
        synchs.add(synchronization);
    }
TransactionSynchronizationManager的registerSynchronization方法会把TransactionSynchronization注册到当前线程的synchronizations

processCommit

org/springframework/transaction/support/AbstractPlatformTransactionManager.java

private void processCommit(DefaultTransactionStatus status) throws TransactionException {
        try {
            boolean beforeCompletionInvoked = false;
            try {
                boolean unexpectedRollback = false;
                prepareForCommit(status);
                triggerBeforeCommit(status);
                triggerBeforeCompletion(status);
                beforeCompletionInvoked = true;
                if (status.hasSavepoint()) {
                    if (status.isDebug()) {
                        logger.debug("Releasing transaction savepoint");
                    }
                    unexpectedRollback = status.isGlobalRollbackOnly();
                    status.releaseHeldSavepoint();
                }
                else if (status.isNewTransaction()) {
                    if (status.isDebug()) {
                        logger.debug("Initiating transaction commit");
                    }
                    unexpectedRollback = status.isGlobalRollbackOnly();
                    doCommit(status);
                }
                else if (isFailEarlyOnGlobalRollbackOnly()) {
                    unexpectedRollback = status.isGlobalRollbackOnly();
                }
                // Throw UnexpectedRollbackException if we have a global rollback-only
                // marker but still didn't get a corresponding exception from commit.
                if (unexpectedRollback) {
                    throw new UnexpectedRollbackException(
                            "Transaction silently rolled back because it has been marked as rollback-only");
                }
            }
            catch (UnexpectedRollbackException ex) {
                // can only be caused by doCommit
                triggerAfterCompletion(status, TransactionSynchronization.STATUS_ROLLED_BACK);
                throw ex;
            }
            catch (TransactionException ex) {
                // can only be caused by doCommit
                if (isRollbackOnCommitFailure()) {
                    doRollbackOnCommitException(status, ex);
                }
                else {
                    triggerAfterCompletion(status, TransactionSynchronization.STATUS_UNKNOWN);
                }
                throw ex;
            }
            catch (RuntimeException | Error ex) {
                if (!beforeCompletionInvoked) {
                    triggerBeforeCompletion(status);
                }
                doRollbackOnCommitException(status, ex);
                throw ex;
            }
            // Trigger afterCommit callbacks, with an exception thrown there
            // propagated to callers but the transaction still considered as committed.
            try {
                triggerAfterCommit(status);
            }
            finally {
                triggerAfterCompletion(status, TransactionSynchronization.STATUS_COMMITTED);
            }
        }
        finally {
            cleanupAfterCompletion(status);
        }
    }
    private void triggerAfterCommit(DefaultTransactionStatus status) {
        if (status.isNewSynchronization()) {
            if (status.isDebug()) {
                logger.trace("Triggering afterCommit synchronization");
            }
            TransactionSynchronizationUtils.triggerAfterCommit();
        }
    }
AbstractPlatformTransactionManager的processCommit方法,在提交成功之后触发triggerAfterCommit,这里调用了TransactionSynchronizationUtils.triggerAfterCommit(),注意这里没有try catch,说明triggerAfterCommit的异常最终会抛给调用方

triggerAfterCommit

org/springframework/transaction/support/TransactionSynchronizationUtils.java

public static void triggerAfterCommit() {
        invokeAfterCommit(TransactionSynchronizationManager.getSynchronizations());
    }
    public static void invokeAfterCommit(@Nullable List<TransactionSynchronization> synchronizations) {
        if (synchronizations != null) {
            for (TransactionSynchronization synchronization : synchronizations) {
                synchronization.afterCommit();
            }
        }
    }

 这里遍历synchronizations执行afterCommit方法,如果其中有一个有异常抛出则中断

小结

使用TransactionSynchronizationManager.registerSynchronization可以在当前线程的事务注册一个TransactionSynchronizationAdapter,可以在afterCommit方法也就是事务提交成功之后执行一些额外逻辑;注意这里抛出的异常不影响事务提交,但是异常不会被catch需要由调用方处理,对于afterCommit有数据库相关操作的建议使用PROPAGATION_REQUIRES_NEW这个事务传播级别,不然afterCommit的db操作可能不会生效。

在事务提交之后做一些事情可能不需要TransactionSynchronizationManager.registerSynchronization这种方式也能实现,也就是需要额外一层来调用事务操作,有异常会抛出,没有异常则执行事务提交之后的事情,前提就是事务回滚异常不能被吞掉,不然外层调用可能以为事务成功了还有一种方式就是使用TransactionalEventListener,这种方式比TransactionSynchronizationManager.registerSynchronization更为优雅一些

doc

以上就是spring的TransactionSynchronizationAdapter事务源码解析的详细内容,更多关于spring TransactionSynchronizationAdapter的资料请关注脚本之家其它相关文章!

相关文章

  • SpringBoot中@ConfigurationProperties 配置绑定

    SpringBoot中@ConfigurationProperties 配置绑定

    本文主要介绍了SpringBoot中@ConfigurationProperties 配置绑定,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • Spring集成事务代码实例

    Spring集成事务代码实例

    这篇文章主要介绍了Spring集成事务代码实例,pring事务的本质其实就是数据库对事务的支持,使用JDBC的事务管理机制,就是利用java.sql.Connection对象完成对事务的提交,需要的朋友可以参考下
    2023-10-10
  • Java Hibernate中使用HQL语句进行数据库查询的要点解析

    Java Hibernate中使用HQL语句进行数据库查询的要点解析

    HQL是Hibernate框架中提供的关系型数据库操作脚本,当然我们也可以使用原生的SQL语句,这里我们来看一下在Java Hibernate中使用HQL语句进行数据库查询的要点解析:
    2016-06-06
  • Java中常见的陷阱题及答案

    Java中常见的陷阱题及答案

    在电脑里找到一份当时学习JAVA时的笔记,看到一些现在已经遗忘的细节。稍微整理了几个,发出来与大家分享。这篇文章主要介绍了Java中常见的陷阱题及答案,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-01-01
  • 聊聊@Autowired注解注入,写接口名字还是实现类的名字

    聊聊@Autowired注解注入,写接口名字还是实现类的名字

    这篇文章主要介绍了聊聊@Autowired注解注入,写接口名字还是实现类的名字,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • 利用Spring框架为自己的校园卡充值(推荐)

    利用Spring框架为自己的校园卡充值(推荐)

    这篇文章主要介绍了利用Spring框架为自己的校园卡充值,本次实验主要运用了Spring的控制反转(IOC)和依赖注入(DI)等知识,通过利用Spring框架编写Java程序,实现学生卡的单次充值,如需对学生卡进行多次充值,可将部分代码修改即可实现,需要的朋友可以参考下
    2022-10-10
  • SpringBoot整合WebSocket实现后端向前端主动推送消息方式

    SpringBoot整合WebSocket实现后端向前端主动推送消息方式

    这篇文章主要介绍了SpringBoot整合WebSocket实现后端向前端主动推送消息方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10
  • java15新功能的详细讲解

    java15新功能的详细讲解

    这篇文章主要介绍了java15的新功能,虽然java15并不是长期支持的版本,但是很多新功能还是很有用的。感兴趣的小伙伴可以参考一下
    2021-08-08
  • 详解Java实现数据结构之并查集

    详解Java实现数据结构之并查集

    并查集这种数据结构,可能出现的频率不是那么高,但是还会经常性的见到,其理解学习起来非常容易,通过本文,一定能够轻轻松松搞定并查集
    2021-06-06
  • feign实现传递参数的三种方式小结

    feign实现传递参数的三种方式小结

    这篇文章主要介绍了feign实现传递参数的三种方式小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06

最新评论