Spring internal transaction

The situation is as follows:

  • Method 1 has four methods for updating the database. Method 1 is annotated using Spring transaction management semantics.

  • Method 2 has a database read method, and it is called after Method1 has completed all of its database updates. Method2 is also annotated using Spring transaction semantics.

  • A web request appears there, the controller intercepts the request and calls method1, and then method2.

  • A transaction is also wrapped around a web request.

I am interested to know:

1. How is Spring known to commit database updates on a successful transaction? Is there a link to a Spring implementation that does transaction management?

2. Since we have a hierarchy of transactions: Transaction around the web request-> Transaction with propagation = RequestNew for Method1-> Transaction with propagation = Required for Method2, how does Spring perform transaction management to ensure that transactions are executed in the right context with the correct order?

In short, it will be great to get a game score for a game about how Spring performs transaction management in all its most fragile details or a link to documentation that doesn't just convey a wave of explanations centered around a JTA or another acronym.

thanks

+6
source share
3 answers

Let's make some basic statements.

  • A transactional context is an environment in which some special properties (database session) are made available to the application runtime, which otherwise are not available. A transaction context is typically used to span a transaction.
  • Spring uses AOP Proxies and XML metadata to achieve declarative transaction management.
  • Annotations are used to indicate Transaction Propagation behavior for a particular method.
  • Spring uses an interceptor mechanism to apply transactional methods.

Here I am reusing the example provided by @stacker above

MyClass{ @Transactional public void sequence() { method1(); method2(); } @Transactional void method1() { } @Transactional(propagation=Propagation.REQUIRES_NEW) void method2() { } } 

You can also implement the same functionality using the xml configuration. Let's consider this as popular and widely used.

During deployment

  • The Spring framework checks the xml configuration files (the famous applicationContext.xml ) and, depending on the configuration, looks at the code for the @Transactional annotation (provided that the configuration is referred to as annotation-based).
  • After that, it generates AOP proxies for the methods marked for the transaction. In simple words, these proxies are nothing more than a wrapper around the appropriate methods.
  • Within these wrapper methods, before and after the code of the transaction advisor is also generated depending on the configuration (namely, the propagation of the transaction).
  • Now that these wrapper methods are called, the transaction advisor enters the image before and after the actual method call.,
  • Representing the same in pseudo code for the example above

      ProxyMyClass{ MyClass myclass; . . . sequence(){ //Transaction Advisor code (Typically begin/check for transaction) myclass.sequence(); //Transaction Advisor code(Typically rollback/commit) } . . . } 

This is how spring manages the transaction. However, a slight simplification.

Now, to answer your questions,

. How is spring known to commit database updates on a successful transaction? Is there a link to a spring implementation that does transaction management?

Whenever you call a method on a transaction, you actually call the proxy server that first executes the transaction advisor (which will start the transaction), then you call the actual business method, as soon as this completes, another transaction advisor (which depends on return path method, commit transaction, or rollback).

Since we have a transaction hierarchy: Transaction around the web request-> Transaction with Propagation = RequestNew for Method1-> Transaction with propagation = Required for Method2, how does spring perform transaction management to ensure that the transactions are executed in the right context with the correct order?

In the case of a transaction hierarchy, the spring structure generates the corresponding checks of the transaction advisor. For example, you mentioned

  • for method 1 (RequestNew), the Transaction Code (or Transaction Advice) should always be to create a new transaction.
  • for method2 (required), the Transaction Advisor code (or transaction advice) will check for an existing transaction and use it if it exists, or create a new transaction.

There is a spring page that summarizes these aspects very well.

Typical Spring Transaction Management

Hope this helps.

+7
source
 Controller @Transactional public void sequence() { method1(); method2(); } @Transactional void method1() { } @Transactional(propagation=Propagation.REQUIRES_NEW) void method2() { } 

The default is REQUIRED (support for the current transaction, creating a new one if it does not exist). Therefore, m1 will use the transaction running in the controller. m2 is annotated as REQUIRES_NEW (create a new transaction, pause the current transaction, if it exists.) The order of the transaction is the order that you call transactional methods.

 Controller begin tx1 |--------------------> m1 (uses tx1) | | begin tx2 |--------------------> m2 (uses tx2) | commit tx2 commit tx1 
+4
source

Have you read the Spring documentation ? AOP is mainly used for transaction management. You should also read the AOP documentation. If the AOP documentation is not enough, I suggest you go through the code. Running code in debug mode with a breakpoint would be nice.

0
source

Source: https://habr.com/ru/post/904368/


All Articles