[Spring Boot] Transactional Propagation Properties
In this article, different propagation properties will be tested.
Test code will make use of two services. To make it easier to distinguish the hierarchy of the two services, I will name them as ParentService
and ChildService
.
ParentService Implementation
To make things simpler, ParentService
simply saves some data and invokes ChildService
like below:
For some transactional propagation properties (MANDATORY, SUPPORTS, NEVER, NOT_SUPPORTED, NESTED), some methods in ParentService
will not be annotated with Transactional
to see the effects of parent transaction.
ChildService Implementation
ChildService
will simply save some data and trigger a runtime exception to see if certain transaction propagation performs rollback or not. Each method will be annotated with a different propagation property to see how each behaves. A sample would look like below:
Transaction Propagation
- REQUIRED (default) : Appends to currently active transaction or creates a new one if there is no active transaction.
- REQUIRES_NEW : Suspends current transaction and creates a new one.
- MANDATORY : Use currently active transaction or throws exception.
- SUPPORTS : Use currently active transaction or execute non-transactional.
- NEVER : Throws exception if there is an active transaction.
- NOT_SUPPORTED : Executes non-transactional whether the current transaction exists or not. If current transaction exists, this will suspend current transaction and execute non-transactional.
- NESTED : Marks a savepoint if there is active transaction or creates a new one if there is no active transaction (like REQUIRED).
Full source:
References: