Distributed Transactions With JTA
Spring Boot 通过使用从 JNDI 中检索的事务管理器来支持跨多个 XA 资源的分布式 JTA 事务。
Spring Boot supports distributed JTA transactions across multiple XA resources by using a transaction manager retrieved from JNDI.
当检测到 JTA 环境时,Spring 的 JtaTransactionManager`用于管理事务。自动配置的 JMS、DataSource 和 JPA Bean 升级为支持 XA 事务。您可以使用标准的 Spring 惯用语(例如 `@Transactional
)来参与分布式事务。如果您在 JTA 环境中,并且仍然想要使用本地事务,可以将 configprop:spring.jta.enabled[] 属性设置为 `false`以禁用 JTA 自动配置。
When a JTA environment is detected, Spring’s JtaTransactionManager
is used to manage transactions.
Auto-configured JMS, DataSource, and JPA beans are upgraded to support XA transactions.
You can use standard Spring idioms, such as @Transactional
, to participate in a distributed transaction.
If you are within a JTA environment and still want to use local transactions, you can set the configprop:spring.jta.enabled[] property to false
to disable the JTA auto-configuration.
Using a Jakarta EE Managed Transaction Manager
如果您将 Spring Boot 应用程序打包为 war`或 `ear`文件,并将其部署到 Jakarta EE 应用程序服务器,则可以使用应用程序服务器内置的事务管理器。Spring Boot 通过查看常见的 JNDI 位置(`java:comp/UserTransaction
、java:comp/TransactionManager`等)来尝试自动配置事务管理器。当使用应用程序服务器提供的事务服务时,您通常还需要确保所有资源都由服务器管理并通过 JNDI 公开。Spring Boot 通过在 JNDI 路径(`java:/JmsXA`或 `java:/XAConnectionFactory
)中查找 ConnectionFactory`来尝试自动配置 JMS,并且您可以使用 configprop:spring.datasource.jndi-name[属性来配置 `DataSource
。
If you package your Spring Boot application as a war
or ear
file and deploy it to a Jakarta EE application server, you can use your application server’s built-in transaction manager.
Spring Boot tries to auto-configure a transaction manager by looking at common JNDI locations (java:comp/UserTransaction
, java:comp/TransactionManager
, and so on).
When using a transaction service provided by your application server, you generally also want to ensure that all resources are managed by the server and exposed over JNDI.
Spring Boot tries to auto-configure JMS by looking for a ConnectionFactory
at the JNDI path (java:/JmsXA
or java:/XAConnectionFactory
), and you can use the configprop:spring.datasource.jndi-name[ property] to configure your DataSource
.
Mixing XA and Non-XA JMS Connections
当使用 JTA 时,主 JMS `ConnectionFactory`Bean 为 XA 感知 Bean,并参与分布式事务。您无需使用任何 `@Qualifier`就可以将其注入到 Bean 中:
When using JTA, the primary JMS ConnectionFactory
bean is XA-aware and participates in distributed transactions.
You can inject into your bean without needing to use any @Qualifier
:
在某些情况下,您可能想要使用非 XA `ConnectionFactory`来处理某些 JMS 消息。例如,您的 JMS 处理逻辑可能比 XA 超时花的时间还要长。
In some situations, you might want to process certain JMS messages by using a non-XA ConnectionFactory
.
For example, your JMS processing logic might take longer than the XA timeout.
如果您想要使用非 XA ConnectionFactory
,可以使用 `nonXaJmsConnectionFactory`Bean:
If you want to use a non-XA ConnectionFactory
, you can the nonXaJmsConnectionFactory
bean:
为了保持一致性,还通过使用 Bean 别名 `xaJmsConnectionFactory`提供了 `jmsConnectionFactory`Bean:
For consistency, the jmsConnectionFactory
bean is also provided by using the bean alias xaJmsConnectionFactory
:
Supporting an Embedded Transaction Manager
{code-spring-boot-src}/jms/XAConnectionFactoryWrapper.java[XAConnectionFactoryWrapper
] 和 {code-spring-boot-src}/jdbc/XADataSourceWrapper.java[XADataSourceWrapper
] 接口可用于支持嵌入式事务管理器。这些接口负责包装 `XAConnectionFactory`和 `XADataSource`Bean,并将其公开为常规 `ConnectionFactory`和 `DataSource`Bean,它们可以透明地加入分布式事务。前提条件是,您的 `ApplicationContext`中注册了 DataSource 和 JMS 自动配置使用 JTA 变体以及 `JtaTransactionManager`Bean 和适当的 XA Wrapper Bean,它们才能正常使用。
The {code-spring-boot-src}/jms/XAConnectionFactoryWrapper.java[XAConnectionFactoryWrapper
] and {code-spring-boot-src}/jdbc/XADataSourceWrapper.java[XADataSourceWrapper
] interfaces can be used to support embedded transaction managers.
The interfaces are responsible for wrapping XAConnectionFactory
and XADataSource
beans and exposing them as regular ConnectionFactory
and DataSource
beans, which transparently enroll in the distributed transaction.
DataSource and JMS auto-configuration use JTA variants, provided you have a JtaTransactionManager
bean and appropriate XA wrapper beans registered within your ApplicationContext
.