Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Transactional Annotation class or method

Trying to collect and understand the main points of @Transactional annotation and crossed one point. So, the main things that we need to keep in mind when using Transactional annotation are:

  1. Transactional annotation can be applied only to public methods [according to Does Spring @Transactional attribute work on a private method?
  2. Transactional annotation should be applied to the concrete classes instead of interfaces [according to Where should I put @Transactional annotation: at an interface definition or at an implementing class?
  3. Transactional annotation should be applied on the Service level [according to Spring @Transactional Annotation Best Practice
  4. If you want to exclude some public method from being annotated with Transactional when whole class is annotated you can use some propagation strategies [according to Transactional annotation on whole class + excluding a single method

Unfortunately I didn't find the answer to the question: Where it is better to put Transactional annotation - to the class or method? We can consider different situations, but mostly i am interested in the one when we have several methods which must have this annotation and some which don't.

Plus, maybe you would like to add some points to this list, that would be really great.

like image 868
Rufi Avatar asked Sep 28 '22 13:09

Rufi


People also ask

Can we use @transactional on class?

The @Transactional Annotation With transactions configured, we can now annotate a bean with @Transactional either at the class or method level: @Service @Transactional public class FooService { //... }

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.

Is it sufficient to annotate the classes with the @transactional annotation?

You can place the @Transactional annotation before an interface definition, a method on an interface, a class definition, or a public method on a class. However, the mere presence of the @Transactional annotation is not enough to activate the transactional behavior.

Why @transactional annotation is used in Spring?

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.


1 Answers

Here is a list of pros and (implicitly) cons I can think of.

Pro Method Level:

  • Easy to read: You look at a method and can tell that it is transactional without having to look at the class, its implemented interfaces or super-classes.
  • Explicit: The annotation clearly tells you that the method is meant to be transactional. It is not just transactional because all methods in the class are transactional.
  • Less unintended merging of (otherwise) independent transactions: If you call multiple transactional methods from a method that is (implicitly) transactional, then the outer transaction defines the whole transaction (unless the propagation settings are like REQUIRES_NEW or so). While this is in many cases no problem, I have seen projects getting into serious trouble due to this in the long run. Especially when it comes to pessimistic locking, it is crucial to keep transactions as independent and small as possible, so transactions need only few locks and the locks are released as soon as possible.

Pro Class Level:

  • Less repetitive: If you put the annotation on the class level, you do not have to annotate each and every transactional method. Some people argue that putting the annotation on each transactional method is against the DRY principle. However, IMHO, if this is against the DRY principle, then Java's private/protected/public modifiers are, too.
like image 55
yamass Avatar answered Oct 01 '22 01:10

yamass