Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")   
}
like image 797
Jayu Dukii Avatar asked Feb 25 '15 12:02

Jayu Dukii


People also ask

What is @transactional propagation propagation required?

Propagation. REQUIRED is the default setting of a @Transactional annotation. The REQUIRED propagation can be interpreted as follows: If there is no existing physical transaction, then the Spring container will create one.

What happens when a method annotated with @transaction propagation Requires_new is invoked?

The main difference between them is if a method in Spring Business Activity/DAO class is annotated with Propagation. REQUIRES_NEW, then when the execution comes to this method, it will create a new transaction irrespective of the existing transaction whereas if the method is annotated with Propagation.

What is use of @transactional why it should be used on service layer?

You should use @Transactional at service layer, if you want to change the domain model for client B where you have to provide the same data in a different model,you can change the domain model without impacting the DAO layer by providing a different service or by creating a interface and implementing the interface in ...

What is the use of propagation in transaction?

Propagation is the ability to decide how the business methods should be encapsulated in both logical or physical transactions. Spring REQUIRED behavior means that the same transaction will be used if there is an already opened transaction in the current bean method execution context.


1 Answers

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.

like image 125
manish Avatar answered Jan 01 '23 23:01

manish