Home > Enterprise >  Spring: A method who call a transactional method has to be transactional?
Spring: A method who call a transactional method has to be transactional?

Time:11-25

It has probably already been asked, but I didn't find the answer so I'm asking it.

If I have this pattern:

@Transactional
private methodA(List<Long> ids) {
   // Call to DB
}

public methodB(List<Long> ids) {
   // Operations that doesn't require DB calls
   this.methodA(ids);
   // Operations that doesn't require DB calls
}

Should Method B be transactional? And if not, will it make shorter the transaction duration?

CodePudding user response:

The transaction is implemented through a proxy. Calls from within the object (such as code within methodB) don't go through the proxy.

You can have one nontransactional service, and one transactional service, and inject the transactional service into the other, so you can call a transactional method from the nontransactional service and go through the proxy. That way you can keep the boundaries you want on your transaction.

  • Related