@Transactional method calling another method without @Transactional anotation?

goe picture goe · Jun 3, 2011 · Viewed 69.8k times · Source

I've seen a method in a Service class that was marked as @Transactional, but it was also calling some other methods in that same class which were not marked as @Transactional.

Does it mean that the call to separate methods are causing the application to open separate connections to DB or suspend the parent transaction, etc?

What's the default behavior for a method without any annotations which is called by another method with @Transactional annotation?

Answer

Arun P Johny picture Arun P Johny · Jun 3, 2011

When you call a method without @Transactional within a transaction block, the parent transaction will continue to the new method. It will use the same connection from the parent method(with @Transactional) and any exception caused in the called method(without @Transactional will cause the transaction to rollback as configured in the transaction definition.

If you call a method with a @Transactional annotation from a method with @Transactional within the same instance, then the called methods transactional behavior will not have any impact on the transaction. But if you call a method with a transaction definition from another method with a transaction definition, and they are in different instances, then the code in the called method will follow the transaction definitions given in the called method.

You can find more details in the section Declarative transaction management of spring transaction documentation.

Spring declarative transaction model uses AOP proxy. so the AOP proxy is responsible for creation of the transactions. The AOP proxy will be active only if the methods with in the instance are called from out side the instance.