ElPiter
ElPiter

Reputation: 4324

Spring @Transactional Service. What happens if it calls another @Transactional Service?

Using Spring, I am reaching the following scenario.

I have a service that has to be @Transactional because it calls several DAOs. But it also calls other services that are already @Transactional.

Somehow, I will be calling nested @Transactional services.

Will Spring manage well?

Upvotes: 4

Views: 2431

Answers (1)

mentallurg
mentallurg

Reputation: 5207

@Transactional without any explicit parameters uses propagation = REQUIRED. This means:

  • If there is no transaction in the current thread, a new transaction will be created
  • If there is a transaction, it will be used

The 2nd service (the nested one) annotated with @Transactional will be executed in the same transaction as the 1st one (the outer one). You don't need to do anything.

In some cases if you want to explicitly separate the nested call from the outer one you can use propagation = REQUIRES_NEW.

Upvotes: 3

Related Questions