ex3v
ex3v

Reputation: 3566

Spring @Transactional on class vs on method

I'm using Spring Boot and Spring Data JPA.

Having following class:

import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation = Propagation.REQUIRED)
public class Foo{

    public void bar(){}

}

will bar() and any other member method be also transactional?

I also have second question. On many tutorials people tend to do something like this:

import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class Foo{

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void bar(){}

}

Why? Is using readOnly true and false a it matter of security?

Upvotes: 9

Views: 14482

Answers (1)

Ankur Singhal
Ankur Singhal

Reputation: 26077

The annotation at the method level completely overrides the annotation at the type-level.

The @Transactional annotation on the class level will be applied to every method in the class.

However, when a method is annotated with @Transactional, this will take precedence over the transactional settings defined at the class level.

Upvotes: 20

Related Questions