0

I have read the official documentation of Spring about the

@Transactional(Propagation.REQUIRED)

annotation but still have some doubts. I will show you an example about how I thinks it behaves:

First Service

public class MyServiceImpl implements MyService{

@AutoWired
private OtherService otherService;

@Transactional(Propagation.REQUIRED)
 public void saveItem(Item item){.....}

@Transactional(Propagation.REQUIRED)
  public void updateItem(Item item){....}
}

@Transactional(Propagation.REQUIRED)
public void deleteItem(Item item){
   otherService.checkItem(item);
...........
 }
}

Second Service

 public class OtherServiceImpl implements OtherService  {



    @Transactional(Propagation.REQUIRED)
    public void checkItem(Item item){.....}


 }

Making calls to MyServiceImpl class from a Spring Controller:

  1. If I make one call to saveItem(), a new physical and logical transaction will be created, right?

  2. If I make two calls to this service from the controller, one to saveItem() and the next to updateItem(),Spring will create for each method two physical different transactions, right?

  3. If I make a call to deleteItem(), only one physical transaction will be created because it will be opened a transaction when deleteItem is called but the inner call from this method to otherService.checkItem() will reuse the first physical transaction, right?

Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
fernando1979
  • 1,727
  • 2
  • 20
  • 26

1 Answers1

0

REQUIRED means that one transaction is needed for running the method, so if one is not already ongoing at the beggining of the method then a new one is created (REQUIRED is the default propagation mode):

1) not necessarilly, if this was called from a method that already had an ongoing transaction

2) depends if the controller is transactional. It should not be by convention, only the service layer should define the scope of the transactions. so in the usual case of a non transactional controller you would have two transactions.

3) depends if where the call was made a transaction was already ongoing. if so then both methods would join a new transaction, if not delete item would create a transaction and otherService keep using it.

Angular University
  • 42,341
  • 15
  • 74
  • 81