Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Transactional annotation

is there any difference between:

  • add "@Transactional" annotation to whole class
  • add for every single method "@Transactional" annotation ?

Using spring and Hibernate ?

like image 636
zaxx Avatar asked Sep 20 '11 08:09

zaxx


People also ask

What does @transactional annotation do?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

What is @transactional used for?

By using @Transactional , many important aspects such as transaction propagation are handled automatically. In this case if another transactional method is called by businessLogic() , that method will have the option of joining the ongoing transaction.

Why do we use @transactional in Spring boot?

So when you annotate a method with @Transactional , Spring dynamically creates a proxy that implements the same interface(s) as the class you're annotating. And when clients make calls into your object, the calls are intercepted and the behaviors injected via the proxy mechanism.

What is transactional annotation in Java?

transaction. Transactional annotation provides the application the ability to declaratively control transaction boundaries on CDI managed beans, as well as classes defined as managed beans by the Java EE specification, at both the class and method level where method level annotations override those at the class level.


1 Answers

Basically, if you annotate the class with @Transactional all methods will be transactional. If you don't, you can annnotate as @Transactional only those methods you want to. Additionally, you can specify different attributes for each method, like isolation, propagation, timeout, ...

Also, take a look at this question, this is a possible duplicate of it: Hibernate transaction annotations in source — difference between class and method level use?

like image 175
Xavi López Avatar answered Sep 24 '22 06:09

Xavi López