Understanding the Spring Framework’s Declarative Transaction Implementation
仅仅告诉您使用 @Transactional
注解注释您的类、将 @EnableTransactionManagement
添加到您的配置,并期望您了解其工作原理是不够的。为了提供更深入的了解,本节将在与事务相关的问题的上下文中阐述 Spring 框架声明式事务基础设施的内部工作原理。
It is not sufficient merely to tell you to annotate your classes with the
@Transactional
annotation, add @EnableTransactionManagement
to your configuration,
and expect you to understand how it all works. To provide a deeper understanding, this
section explains the inner workings of the Spring Framework’s declarative transaction
infrastructure in the context of transaction-related issues.
了解 Spring 框架声明式事务支持的最重要概念是,此支持在 via AOP proxies 中启用,并且事务建议由元数据(当前基于 XML 或注解)驱动。AOP 与事务元数据的组合产生一个 AOP 代理,它使用 TransactionInterceptor
结合适当的 TransactionManager
实现来在方法调用周围驱动事务。
The most important concepts to grasp with regard to the Spring Framework’s declarative
transaction support are that this support is enabled
via AOP proxies and that the transactional
advice is driven by metadata (currently XML- or annotation-based). The combination of AOP
with transactional metadata yields an AOP proxy that uses a TransactionInterceptor
in
conjunction with an appropriate TransactionManager
implementation to drive transactions
around method invocations.
Spring AOP 涵盖在 the AOP section 中。 |
Spring AOP is covered in the AOP section. |
Spring 框架的 TransactionInterceptor
为命令式和响应式编程模型提供事务管理。拦截器通过检查方法返回类型来检测所需的事务管理方式。返回响应式类型(例如 Publisher
或 Kotlin Flow
,或其子类型)的方法有资格进行响应式事务管理。包括 void
在内的所有其他返回类型都使用代码路径用于命令式事务管理。
Spring Framework’s TransactionInterceptor
provides transaction management for
imperative and reactive programming models. The interceptor detects the desired flavor of
transaction management by inspecting the method return type. Methods returning a reactive
type such as Publisher
or Kotlin Flow
(or a subtype of those) qualify for reactive
transaction management. All other return types including void
use the code path for
imperative transaction management.
事务管理方式会影响所需的交易管理器。命令式事务需要 PlatformTransactionManager
,而响应式事务使用 ReactiveTransactionManager
实现。
Transaction management flavors impact which transaction manager is required. Imperative
transactions require a PlatformTransactionManager
, while reactive transactions use
ReactiveTransactionManager
implementations.
由 A reactive transaction managed by 在使用 When configured with a |
下图显示了对事务代理的方法调用的概念视图:
The following image shows a conceptual view of calling a method on a transactional proxy: