Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Global transaction vs Local transaction

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

  • 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.

like image 411
18bytes Avatar asked Mar 06 '14 10:03

18bytes


People also ask

What is the difference between a local and a global transaction?

Normal transaction is also known as Local transaction. A global transaction is a mechanism that allows a set of programming tasks, potentially using more than one resource manager and potentially executing on multiple servers, to be treated as one logical unit.

What is global transaction in Spring?

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)

What is the difference between Spring transaction and hibernate transaction?

Spring framework is useful for transaction management, dependency injection; aspect-oriented programming for applications whereas Hibernate framework is useful for object-relational persistence, access data layers, and query retrieval services for enterprise-level applications.

What are the default @transactional settings?

The default @Transactional settings are: The propagation setting is PROPAGATION_REQUIRED. The isolation level is ISOLATION_DEFAULT. The transaction is read/write.


1 Answers

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

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)

Answering your later question. The documentation says that JpaTransactionManager is 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 DataSourceTransactionManager should be used instead.

like image 93
Serhiy Avatar answered Oct 07 '22 16:10

Serhiy