Tuesday, June 29, 2021

Transaction Propagation Types in Spring

Transaction Propagation Types in Spring



There are 7 types of propagation types:

1. Propagation.REQUIRED

@Transactional(propagation=Propagation.REQUIRED)

- This is default propagation type.
- If there is already an active transaction, then this propagation does not do anything and uses that transaction scope only. In another case, where there is not any active transaction, then it creates new transaction.



2. Propagation.SUPPORTS

@Transactional(propagation=Propagation.SUPPORTS)
 
- If there is already an active transaction, then this propagation does not do anything and uses that transaction scope only. Otherwise, this method runs non-transactional.



3. Propagation.MANDATORY

 @Transactional(propagation=Propagation.MANDATORY)
 
- If there is already an active transaction, then this propagation does not do anything and uses that transaction scope only. Otherwise throws an IllegalTransactionStateException.



4. Propagation.NEVER

@Transactional(propagation=Propagation.NEVER)
 
 - If there is already an active transaction, then this propagation throws an IllegalTransactionStateException.
 
 
 

5. Propagation.NOT_SUPPORTED

@Transactional(propagation=Propagation.NOT_SUPPORTED)
 
 - If there is already an active transaction, then this propagation suspends that transaction and runs non-transactional.


 

6. Propagation.REQUIRES_NEW

@Transactional(propagation=Propagation.REQUIRES_NEW)
 
 - If there is already an active transaction, then this propagation suspends that transaction and creates a new transaction.
- In another case of not having any active transaction, this creates new transaction.


 

7. Propagation.NESTED

@Transactional(propagation=Propagation.NESTED)
 
 - If there is already an active transaction, then spring marks a savepoint, so if the method throws any exception then transaction gets rollback to this savepoint.
- In another case of not having any active transaction, works as Propagation.REQUIRED.
























No comments:

Post a Comment