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

To make things simpler, ParentService simply saves some data and invokes ChildService like below:

ParentService Implementation

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 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:

ChildService Implementation
  • 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).

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store