spring-boottransactional

Compilation error when using @Transactional with propagation attribute


I developed a spring data jpa program using this tutorial. Then modified it by adding a new class/method to test spring's @Transactional annotation.

@Transactional
public void txnMethod() {
    repository.save(new Customer("First Customer",""));
    repository.save(new Customer("Second Customer",""));
    ...
}

The above code compiled and executed correctly. Then I modified the code to explictly set propagation mode as shown below, but this gives me a compilation error - "The attribute propagation is undefined for the annotation type Transactional"

@Transactional(propagation=Propagation.REQUIRED)
public void txnMethod() {
    repository.save(new Customer("First Customer",""));
    repository.save(new Customer("Second Customer",""));
    ...
}

How can I specify the propagation mode explicitly ? Below are the dependencies in build.gradle. Am using spring boot version 1.2.1.RELEASE

dependencies {
    compile("org.springframework.boot:spring-boot-starter-jdbc")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile ("org.springframework.boot:spring-boot-starter-tomcat")   
    compile("com.h2database:h2")                                      
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")   
}

Solution

  • When working with applications that have a direct or transitive dependency on Spring Data, two classes with the name @Transactional are available on the compile-time classpath. One of these is javax.persistence.Transactional and the other one is org.springframework.transaction.annotation.Transactional. It is the later class that must be used for declarative transaction management with Spring. The enumeration Propagation is also supported only by the later. The former supports a different enumeration called TxType.

    Do ensure that the @Transactional you are applying is of the type org.springframework.transaction.annotation.Transactional as IDEs sometimes add an import for javax.persistence.Transactional while the user is typing @Transactional. Then, attempting to add Propagation to the annotation fails because javax.persistence.Transactional does not support this enumeration.