Suppose I have a Repository class.
@Repository
class MyRepository {
@Transactional
void method1 () {
// some logic here
}
void method2 () {
// some logic here
method1();
// some logic here
}
}
Is it possible to do that in String? And how this works?
The answer your question is no - @Transactional will have no effect if used to annotate private methods. The proxy generator will ignore them. When using proxies, you should apply the @Transactional annotation only to methods with public visibility.
1 Answer. Show activity on this post. If you call method2() from method1() within the same class, the @Transactional annotation of the second method will not have any effect because it is not called through proxy, but directly.
If you call a method with a @Transactional annotation from a method with @Transactional belonging to the same Spring Bean , then the called methods transactional behavior will not have any impact on the transaction.
Annotation Type Transactional. Describes a transaction attribute on an individual method or on a class. When this annotation is declared at the class level, it applies as a default to all methods of the declaring class and its subclasses.
This does not work, since this a self call. See
Spring @Transaction method call by the method within the same class, does not work?
Depending on your application and its responsibilities, you could create another bean for method2()
.
Apart from that, DAO methods should usually not be annotated @Transactional
. See
Where does the @Transactional annotation belong?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With