Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Propagation examples in layman's terms

The Spring docs do a fantastic job of describing transactional propagation properties.

However, I was wondering if there are any well-known, real-world examples available which describe each of these properties more thoroughly in layman's terms?

like image 912
wild_nothing Avatar asked Aug 01 '14 09:08

wild_nothing


People also ask

What is propagation in Spring?

Propagation is the ability to decide how the business methods should be encapsulated in both logical or physical transactions. Spring REQUIRED behavior means that the same transaction will be used if there is an already opened transaction in the current bean method execution context.

What are the propagation and isolation levels do you know in Spring?

We can set the isolation level of a transaction by @Transactional::isolation. It has these five enumerations in Spring: DEFAULT, READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE.

What is the use of propagation in transaction?

Transaction Propagation indicates if any component or service will or will not participate in transaction and how will it behave if the calling calling component/service already has or does not have a transaction created already.

What is the difference between required and Requires_new?

REQUIRED, spring checks for any existing transaction. If yes, it uses that old transaction otherwise it creates a new one. But, the disadvantage of Propagation. REQUIRES_NEW is that even if the inner method fails to execute (because of some exception), the outer method commits the transaction.


1 Answers

PROPAGATION_REQUIRED

class Service {     @Transactional(propagation=Propagation.REQUIRED)     public void doSomething() {         // access a database using a DAO     } } 

When doSomething() is called it will start a new transaction if the caller has not already started a transaction.

If the caller of this method has already started a transaction then the callers' transaction is used and no new transaction is created (i.e. there is one transaction in play).

If an exception is thrown inside doSomething() then it will be rolled back, meaning that the caller will also see the transaction rolled back.

When doSomething() returns the transaction will not have been commited yet. It is the caller that will commit the transaction (or possibly rolled-back).

PROPAGATION_REQUIRES_NEW

class Service {     @Transactional(propagation=Propagation.REQUIRES_NEW)     public void doSomething() {         // access a database using a DAO     } } 

When doSomething() is called it will always start a new transaction.

If the caller of this method has already started a transaction (TxnOuter) then the callers' transaction is suspended and a new transaction (TxnInner) is created (i.e. there are two transactions in play).

If an exception is thrown inside doSomething() then TxnInner will be rolled back, but the "suspended" transaction from the caller (TxnOuter) is unaffected.

When doSomething() returns without an Exception it will commit the transaction (TxnInner). The caller's transaction (TxnOuter) will be resumed and be unaware that another transaction was commited. The caller can then commit or roll-back TxnOuter as it sees fit.

The important point to note is that the Database views TxnOuter and TxnInner as completely independant transactions, and therefore two independant commits.

PROPAGATION_NESTED

class Service {     @Transactional(propagation=Propagation.NESTED)     public void doSomething() {         // access a database using a DAO     } } 

NESTED can only be used if your JDBC driver and/or database supports JDBC savepoints

When doSomething() is called it will start a new transaction if the caller has not already started a transaction.

If the caller of this method has already started a transaction then the callers' transaction is used and no new transaction is created (i.e. there is one transaction in play). However a "savepoint" is marked on the transaction when doSomething() is entered.

If an Exception is thrown inside doSomething() then the transaction can be partially rolled back the transaction to the "savepoint". The caller will continue with the transaction.

When doSomething() returns without an Exception it is the caller who will commit the entire transaction (or roll back).

The important point to note is that the Database views only one transaction and there is only one commit.

like image 145
Brad Avatar answered Oct 13 '22 16:10

Brad