Home spring What is the difference between @Transactional definition in class and method (Spring)

What is the difference between @Transactional definition in class and method (Spring)




I found information on the net that the difference between the installation location of the @Transactional annotation is that:

When a transaction is set over a class, if a exception is thrown then rollback works.
If the annotation is set over the method, then rollback does not work.

When the annotation is applied at the class level (class-level ), then the transaction will be applied to all public methods of this class, over which there is no given transaction (that is, a transaction over the class is sufficient.)
But this only works for methods with the public modifier.
Methods with modifiers – private, default-package and protected will be ignored.

Each service class object is wrapped in a proxy . This mode is enabled by default

Annotation @Transactional set above a class will apply to every method in this class, except for the method with the private modifier.

But if in this class over some method its own
annotation @Transactional , then such an annotation for this method will be
take precedence over annotation set over class.

However , if this method is called from an external class , then
the annotation over the class will take precedence.

Are these statements true (for Spring 5.x)?

Is this so and are there any other differences?

Answer 1, authority 100%

If you have any questions, you can always refer to official documentation :

At the class level, this annotation applies as a default to all methods of the declaring class and its subclasses. Note that it does not apply to ancestor classes up the class hierarchy; methods need to be locally redeclared in order to participate in a subclass-level annotation.

Speaking in Russian, the annotation above the class is the transactional behavior that will be applied in the methods of this class and its descendants by default .

The quote in the question is partially incorrect information.

However, there is another topic nearby. This is the mechanism through which the “magic” of transactions appears.

To implement this magic, wrapping the original java object into another java object with the same external API is used. This is exactly what a proxy is. It is important to understand that this proxy belongs to a dynamically generated class, i.e. a class made from bytecode that was created during program execution, not compilation.

This very “wrapping the source java object in another java object, with the same external API ” can be implemented in two ways:

  1. Create a derived class from the original class. Hello cglib ;
  2. Create a class that implements all the interfaces of the original class. Hello native JVM engine .

Since private (and static ) methods cannot be overridden / implemented from the parent / interface class, annotations of this kind over them will not be of any use at all in all cases. Scientifically speaking, “magic” can only appear in places where there is a virtual challenge. A virtual call is a call during which it is determined in the context of which object (the same this ) it will be made.

Let’s say we have:

interface Api {
  void call ();
  void anotherCall ();
  void toPrivateCall ();
class ApiImpl implements Api {
  void call () {
   // (i1)
Void Anothercall () {
   // (i2)
   this.call ();
  Void Toprivatecall () {
   // (i3)
   this.privatecall ();
  Private void privatecall () {
   // (i4)

and calls

@ component
  void test () {
   api.call (); // (C1)
   API.anothercall (); // (C2)
   api.toprivatecall (); // (C3)

Result by chain chains depending on technology:


Proxy transaction through the heir class

transaction for proxy via interfaces

C1 – & GT; I1

will appear between C1 – & GT; I1

will appear between C1 – & GT; I1

C2 – & GT; I2 – & gt; I1

will appear between I2 – & GT; I1

will not appear

C3 – & GT; i3 – & gt; I4

will not appear

will not appear

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions