Translation of SpringBoot @Transactional

Viewed 110

In my code I have an interface with an updatePerson(Age age, Address address). This method is being called from 3 different controllers. Now because of some requirement in 1 of the controller I had to pass an additional field to this method as updatePerson(Age age, Address address, Experience experience).

In order to avoid passing updatePerson(Age age, Address address, null) in the 2 other controllers, I thought of converting this method as a default method in my interface and internally calling the other method. So my interface now looks like:

public interface PersonService
{
   default PersonDO updatePerson(Age age, Address address) {
     return updatePerson(Age age, Address address, null);
   }

   PersonDO updatePerson(Age age, Address address, Experience experience);
}

Now the issue is : In the implementation of this interface, the original updatePerson was annotated with @Transactional. I know if a normal method calls a method annotated with @Transactional, the transaction doesn't work as it is taken care of by spring.

So in the implementation now I am having updatePerson(Age age, Address address, Experience experience) annotated with @Transactional.

The controller which directly calls updatePerson(Age age, Address address, Experience experience) , the execution will happen in a transaction. But I am not sure what happens when the controller which calls the default updatePerson(Age age, Address address) and it internally calls implemented method ? Does the execution still happens in a transaction or not ?

1 Answers

You can add @Transactional to default method and therefore it will be inside transaction also

@Transactional    
default PersonDO updatePerson(Age age, Address address) {
  return updatePerson(Age age, Address address, null);
}
Related