Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to invoke transactional method from non transactional method in Spring?

Tags:

java

spring

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?

like image 974
JohnWinter Avatar asked Jun 16 '15 13:06

JohnWinter


People also ask

Can we use @transactional on private methods?

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.

What happens if one @transactional annotated method is calling another @transactional annotated method on the same object instance?

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.

What happens if a method annotated with @transactional calls another method annotated with @transactional?

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.

Can @transactional annotation only be used at class level?

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.


1 Answers

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?

like image 74
user140547 Avatar answered Sep 20 '22 13:09

user140547