Detecting If a Spring Transaction Is Active – 检测一个Spring事务是否处于活动状态

最后修改: 2020年 10月 16日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Detecting transactions could be useful for audit purposes or for dealing with a complex code base where good transaction conventions weren’t implemented.

检测交易对于审计目的或处理没有实施良好交易惯例的复杂代码库是很有用的。

In this brief tutorial, we’ll go over a couple of ways to detect Spring transactions in our code.

在这个简短的教程中,我们将介绍几种在代码中检测Spring事务的方法。

2. Transaction Configuration

2.事务配置

In order for transactions to work in Spring, transaction management must be enabled. Spring will enable transaction management by default if we’re using a Spring Boot project with spring-data-* or spring-tx dependencies. Otherwise, we’ll have to enable transactions and provide a transaction manager explicitly.

为了让事务在Spring中工作,必须启用事务管理。如果我们使用的Spring Boot项目有spring-data-*或spring-tx依赖,Spring将默认启用事务管理。否则,我们就必须启用事务,并明确提供一个事务管理器。

First, we need to add the @EnableTransactionManagement annotation to our @Configuration class. This enables Spring’s annotation-driven transaction management for our project.

首先,我们需要将@EnableTransactionManagement注释添加到我们的@Configuration类。这就为我们的项目启用了Spring的注解驱动的事务管理。

Next, we must provide either a PlatformTransactionManager or a ReactiveTransactionManager bean. This bean requires a DataSource. We could choose to use a number of common libraries, such as those for H2 or MySQL. Our implementation doesn’t matter for this tutorial.

接下来,我们必须提供一个PlatformTransactionManager或者一个ReactiveTransactionManagerbean。这个Bean需要一个DataSource。我们可以选择使用一些常见的库,例如H2或MySQL的库。我们的实现对本教程来说并不重要。

Once we enable transactions, we can use the @Transactional annotation to generate transactions.

一旦我们启用了事务,我们就可以使用@Transactional注释来生成事务。

3. Using TransactionSynchronizationManager

3.使用TransactionSynchronizationManager

Spring has provided a class called TransactionSychronizationManager. Thankfully, this class has a static method that allows us to know whether we are in a transaction, called isActualTransactionActive().

Spring提供了一个名为TransactionSychronizationManager的类。值得庆幸的是,这个类有一个静态方法,可以让我们知道我们是否在一个事务中,这个方法叫做isActualTransactionActive()

To test this, let’s annotate a test method with @Transactional. We can assert that isActualTransactionActive() returns true:

为了测试这一点,让我们用@Transactional来注释一个测试方法。我们可以断言isActualTransactionActive()返回true

@Test
@Transactional
public void givenTransactional_whenCheckingForActiveTransaction_thenReceiveTrue() {
    assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
}

Similarly, the test should assert that false is returned when we remove the @Transactional annotation:

同样地,测试应该断言,当我们删除@Transactional注解时,会返回false

@Test
public void givenNoTransactional_whenCheckingForActiveTransaction_thenReceiveFalse() {
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
}

4. Using Spring Transaction Logging

4.使用Spring事务日志

Perhaps we don’t need to programmatically detect a transaction. If we would rather just see when a transaction happens in our application’s logs, we can enable Spring’s transaction logs in our properties file:

也许我们不需要以编程方式检测事务。如果我们宁愿只在应用程序的日志中看到事务发生的时间,我们可以在属性文件中启用Spring的事务日志

logging.level.org.springframework.transaction.interceptor = TRACE

Once we enable that logging level, transaction logs will start appearing:

一旦我们启用该日志级别,交易日志将开始出现。

2020-10-02 14:45:07,162 TRACE - Getting transaction for [com.Class.method]
2020-10-02 14:45:07,273 TRACE - Completing transaction for [com.Class.method]

These logs won’t offer very helpful information without any context. We can simply add some of our own logging and we should easily be able to see where transactions are happening in our Spring-managed code.

这些日志在没有任何背景的情况下不会提供非常有用的信息。我们可以简单地添加一些我们自己的日志,我们应该能够很容易地看到在我们的Spring管理的代码中发生的事务。

5. Conclusion

5.总结

In this article, we saw how to check whether a Spring transaction is active. We learned how to programmatically detect transactions using the TransactionSynchronizationManager.isActualTransactionActive() method. We also discovered how to enable Spring’s internal transaction logging in case we want to see transactions in our logs.

在这篇文章中,我们看到了如何检查一个Spring事务是否处于活动状态。我们学习了如何使用TransactionSynchronizationManager.isActualTransactionActive()方法以编程方式检测事务。我们还发现了如何启用Spring的内部事务日志,以防我们想在日志中看到事务。

As always, code examples can be found over on GitHub.

一如既往,代码示例可以在GitHub上找到