I have method M with @Transactional in service A.
I have service B extends A
with overrided method M.
Will be overrided method M still transactional? Or I should add there @Transactional?
I have method M with @Transactional in service A.
I have service B extends A
with overrided method M.
Will be overrided method M still transactional? Or I should add there @Transactional?
What you are actually asking : is the @Transactional annotation on the method inherited.
Short answer : no. Annotations on methods are never inherited.
Long answer : see this post.
Most of the time, you can get away with a single @Transactional at the class level. But sometimes you need to customize just one method to behave differently. In these cases it's still redundant to mark all other methods with identical @Transactional annotations.
Warp-persist provides a facility where you can override the class's transactional behavior with a specific @Transactional on a particular method if desired:
@Transactional
public class MyRepository {
public void save(Thing t) { .. }
@Transactional(rollbackOn = NoSuchEntityException.class) //optional
public void remove(Thing t) { .. }
public Thing fetch(Long id) { .. }
}
In the example above, save() and fetch() have standard transactional behavior as specified at the class-level. But remove() has a specific rollbackOn clause which is used instead.
Remember that private methods cannot be intercepted for transaction wrapping. This is because you cannot override private methods in subclasses. If any such methods are encountered, they will be silently ignored.
For Spring @Transactional
using proxies (JDK or Cglib) with the default implementation, the method M
will inherit the super class annotation attributes and will be transactional.
We can put the annotation on definitions of interfaces, classes, or directly on methods. They override each other according to the priority order; from lowest to highest we have: interface, superclass, class, interface method, superclass method, and class method.[1]
This behavior happens in the SpringTransactionAnnotationParser
.[2]
All of the previous imply that the invocations are properly intercepted by Spring. Simply speaking, it needs to be a Spring bean, on a public method, and invoked from another class instance.[3]
[1]Transaction Propagation and Isolation in Spring @Transactional
© 2022 - 2024 — McMap. All rights reserved.