In this article, we will understand the different transaction propagation types available in Spring Boot.
Why do we call this Transaction Propagation?
When we call a method from another method then we might want to propagate the existing transaction context or restrict the propagation & create a new context. Hence we call this as transaction propogation.
We use @Transactional annotation of Spring to wrap a method inside a database transaction context.
So, using this @Transactional annotation, we can set our desired propagation type.
This annotation provides us the ability to decide : whether we want to use an existing active transaction or we want to create a new transaction.
The new transaction can be a fresh one (not within any another transaction) or it can be a nested transaction inside an existing active transaction.
By transaction, we mean a transactional context here. So, either we can use an existing context or can start a new context altogether.
There can be multiple use cases when we are calling the method like: we want to use existing/new context or we do not want any context at all etc. Based on these use cases, we have 7 types of propagations available:
- Propagation.REQUIRED
- Propagation.SUPPORTS
- Propagation.MANDATORY
- Propagation.NEVER
- Propagation.NOT_SUPPORTED
- Propagation.REQUIRES_NEW
- Propagation.NESTED