dplesa
dplesa

Reputation: 1425

Starting a new transaction from transactional method

Currently I have a controller which calls @Transactional annotated method in a service A which inserts the entity to a database, and then calls another method in a service B which updates the entity.

I would like to call only the method in a service A, which will then in turn call a method in a service B. First transaction should happen in a method of a service A, and another transaction should happen in a method of a service B.

I have tried to understand @Transactional annotation, its isolation and propagation in order to achieve this, but I couldn't make it work.

Upvotes: 0

Views: 2109

Answers (1)

Mạnh Quyết Nguyễn
Mạnh Quyết Nguyễn

Reputation: 18235

If you want to separate transaction in service B from transaction in service A then you have to start new transaction by using Propagation.REQUIRES_NEW.

Mark your service B method with:

@Transaction(propagation = Propagation.REQUIRES_NEW)

Upvotes: 2

Related Questions