Spring Data — Transactional Caveats

Semyon Kirekov
Level Up Coding
Published in
4 min readJun 7, 2021

--

Spring is the most popular Java framework. It has lots of out-of-box solutions for web, security, caching, and data access. Spring Data especially makes the life of a developer much easier. We don’t have to worry about database connections and transaction management. The framework does the job. But the fact that it hides some important details from us may lead to hard-tracking bugs and issues. So, let’s deep dive into @Transactional annotation.

Default Rollback Behaviour

Assume that we have a simple service method that creates 3 users during one transaction. If something goes wrong, it throws java.util.Exception.

PersonService with possible checked exception

And here is a simple unit test.

PersonServiceTest

Do you think the test will pass or not? Logic tells us that Spring should roll back the transaction due to an exception. So, personRepository.count() ought to return 0, right? Well, not exactly.

expected: <0> but was: <2>
Expected :0
Actual :2

That requires some explanations. By default, Spring rolls back transaction only if an unchecked exception occurs. The checked ones are treated like restorable. In our case, Spring performs commit instead of rollback. That’s why personRepository.count() returns 2.

The easiest way to fix it is to replace a checked exception with an unchecked one (e.g., NullPointerException). Or else we can use the annotation’s attribute rollbackFor.

For example, both of these cases are perfectly valid.

PersonService with possible checked an unchecked exceptions
PersonServiceTest
Test results

Rollback on Exception Suppressing

Not all exceptions have to be propagated. Sometimes it is acceptable to catch it and log information about it.

Suppose that we have another transactional service that checks whether the person can be created with the given name. If it is not, it throws IllegalArgumentException.

PersonValidateService

Let’s add validation to our PersonService.

PersonService with validation

If validation does not pass, we create a new person with the default name.

Ok, now we need to test it.

PersonServiceTest

But the result is rather unexpected.

Unexpected exception thrown: org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only

That’s weird. The exception has been suppressed. Why did Spring roll back the transaction? Firstly, we need to understand the @Transactional management approach.

Internally Spring uses the aspect-oriented programming pattern. Skipping the complex details, the idea behind it is to wrap an object with the proxy that performs the required operations (in our case, transaction management). So, when we inject the service that has any @Transactional method, actually Spring puts the proxy.

Here is the workflow for the defined addPeople method.

PersonServiceTest workflow

The default @Transactional propagation is REQUIRED. It means that the new transaction is created if it’s missing. And if it’s present already, the current one is supported. So, the whole request is being executed within a single transaction.

Anyway, there is a caveat. If the RuntimeException throws out of the transactional proxy, Spring marks the current transaction as rollback only. That’s exactly what happened in our case. PersonValidateService.validateName throws IllegalArgumentException. Transactional proxy tracks it and sets on the rollback flag. Later executions during the transaction have no effect because they ought to be rolled back in the end.

What’s the solution? There are several ones. For example, we can add noRollbackFor attribute to PersonValidateService.

PersonValidateService with “noRollbackFor” attribute

Another approach is to change the transaction propagation to REQUIRES_NEW. In this case, PersonValidateService.validateName will be executed in a separate transaction. So, the parent one will not be rollbacked.

PersonValidateService with “propagation” attribute

Possible Kotlin Issues

Kotlin has many common things with Java. But exception management is not the case.

Kotlin eliminated the idea of checked and unchecked exceptions. Basically, any exception in the language is unchecked because we don’t need to specify throws SomeException in the method declaration. The pros and cons of this decision should be a topic for another story. But now I want to show you the problems it may bring with Spring Data usage.

Let’s rewrite the very first example of the article with java.util.Exception in Kotlin.

PersonService written in Kotlin
PersonServiceTest written in Kotlin

The test fails just like in Java.

expected: <0> but was: <2>
Expected :0
Actual :2

There are no surprises. Spring manages transactions in the same way in either Java or Kotlin. But in Java, we cannot execute a method that throws java.util.Exception without taking care of it. Kotlin allows it. That may bring unexpected bugs, so, you should pay extra attention to such cases.

Conclusion

That’s all I wanted to tell you about Spring @Transactional annotation. If you have any questions or suggestions, please leave your comments down below. Thanks for reading!

--

--

Java Dev and Team Lead. Passionate about clean code, tea, pastila, and smooth jazz/blues. semyon@kirekov.com