Java Spring 全局事务 vs 本地事务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22221741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:24:59  来源:igfitidea点击:

Spring Global transaction vs Local transaction

javaspringtransactionsspring-transactionsdistributed-transactions

提问by 18bytes

While reading through Spring transaction documentation I see that it supports both Global transactions and Local transactions.

在阅读 Spring 事务文档时,我发现它支持全局事务和本地事务。

  • In simple terms what is global transaction and what is local transaction?
  • What are the advantages of one over the other? What are the appropriate uses of them?
  • 简单来说什么是全局事务,什么是本地事务?
  • 一个比另一个有什么优势?它们的适当用途是什么?

If I use the following configuration – does it mean it is a local transaction?

如果我使用以下配置 - 是否意味着它是本地事务?

<tx:annotation-driven transaction-manager="transManager" />

<bean id="transManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf" />
</bean>

I tried searching both in Google and Stackoverflow, but did not get any resources explaining the same in simple terms.

我尝试在 Google 和 Stackoverflow 中进行搜索,但没有得到任何简单解释相同内容的资源。

采纳答案by Serhiy

Actually there are plenty of resources answering your first two questions, for example Spring Documentationexplains what local and global transaction is and depicts their differences in chapter 9.2 Motivation. In few words:

实际上有很多资源可以回答您的前两个问题,例如Spring 文档解释了本地事务和全局事务是什么,并在第9.2Motivation 中描述了它们的区别。简而言之:

Global Transaction is an application server managed transaction, allowing to work with different transactional resources (this might be two different database, database and message queue, etc)

全局事务是一个应用服务器管理的事务,允许使用不同的事务资源(这可能是两个不同的数据库、数据库和消息队列等)

Local Transaction is resource specific transaction (for example Oracle Transactions) and application server has nothing to do with them. (the same chapter explains the pros and cons of each of them very well and much better then I could explain, so I suggest you to give a closer look)

本地事务是特定于资源的事务(例如Oracle 事务),应用服务器与它们无关。(同一章很好地解释了每个人的优缺点,比我解释的要好得多,所以我建议你仔细看看)

Answering your later question. The documentationsays that JpaTransactionManageris capable for processing global transactions, so by looking at the piece of presented code it's hard to say if it's local or global transaction. The same documentation says that local single-resource transaction DataSourceTransactionManagershould be used instead.

回答你后来的问题。该文件说,JpaTransactionManager能够为全球处理事务,所以通过看片的呈现代码很难说,如果是本地或全局事务。相同的文档说DataSourceTransactionManager应该使用本地单一资源事务来代替。