Junior — Middle
Will a new transaction be initiated if a transactional method is called inside another transactional method?
sobes.tech AI
Answer from AI
If one service method calls another method marked as transactional within the same class, a new transaction is generally not created. By default, most frameworks (such as Spring) support a model of "nested" calls, where the inner transaction joins the existing one.
This means that if the outer method is already running within a transaction, calling the inner transactional method will execute within the same transaction, and a new one will not be created.
Example in Java using Spring:
@Transactional
public void outerMethod() {
// code within a transaction
innerMethod(); // will not create a new transaction, but will be part of the current one
}
@Transactional
public void innerMethod() {
// code also within the same transaction
}
If it is required for the inner method to run in a separate transaction, you need to explicitly specify propagation = Propagation.REQUIRES_NEW.