Spring @Transactional inheritance rules Spring @Transactional inheritance rules spring spring

Spring @Transactional inheritance rules


From the spring transaction documentation,

Note: In proxy mode (which is the default), only 'external' method calls coming in through the proxy will be intercepted. This means that 'self-invocation', i.e. a method within the target object calling some other method of the target object, won't lead to an actual transaction at runtime even if the invoked method is marked with @Transactional!

Even though you have the @Transactional on your concrete implementation and you are calling process method which is actually transactional by your annotation, but the process method calling processSpecific on your sub class is not transactional because of this internal call.

Look into Weaving.


Did you read the part about transaction propagation and how it can be configured using @Transactional?

Another area of interest is that Spring recommends that you should annotate concrete classes (as opposes to annotate interfaces).


This is an old question. But I ran into a similar situation and found the explanation in the current javadoc for the @Transactional annotation:

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/Transactional.html

At the class level, this annotation applies as a default to allmethods of the declaring class and its subclasses. Note that it doesnot apply to ancestor classes up the class hierarchy; methods need tobe locally redeclared in order to participate in a subclass-levelannotation.

So, when using inheritance and class level annotation you should annotate the super class and not the sub classes if the super class contains any public methods that should be transactional or do call methods implemented by the sub classes that need to be transactional.

Calling methods which are implemented and annotated @Transactional by the subclasses from methods in the super class without the super class or that method being annotated @Transactional is a bug.And if both are annotated but the attributes on the annotation are inconsistent that is also a bug.

In a good design virtual methods in the super class which are implemented by subclasses should be used by the super class only and they should because of that always have scope protected and never need any @Transactional annotation in the sub class