In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "A detailed introduction to the transaction communication mechanism of spring". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "the detailed introduction of spring transaction communication mechanism"!
Catalogue
Transaction Propagation Mechanism of spring
1 、 why
Why is there a transaction propagation mechanism?
2. Conditions for the effectiveness of the communication mechanism.
Solution
3. The type of communication mechanism
PROPAGATION_REQUIRED (default)
REQUIRES_NEW (usually used in submethods that require separate transactions)
NESTED
SUPPORTS
NOT_SUPPORTED
MANDATORY
NEVER
Transaction Propagation Mechanism of spring
Background: I encountered transaction methods several times during my internship, and once I found that the transaction was not rolled back during a local test, so I wrote a brief description on wx. Today, let's make a self-summary of spring transactions.
1. Why does why have transaction propagation mechanism?
Scenario 1:
The serviceA method calls the serviceB method, but both methods have transactions, so if the serviceB method is abnormal, whether to have the serviceB method commit or roll back both.
Scenario 2:
The serviceA method calls the serviceB method, but only the serviceA method adds a transaction, whether to add serviceB to the serviceA transaction, and whether to roll back the serviceA if the serviceB is abnormal.
Scenario 3:
The serviceA method calls the serviceB method, and both have transactions. The serviceB has been executed normally, but the serviceA exception is required. Whether the data of the serviceB needs to be rolled back.
Therefore, we need to have a corresponding transaction propagation mechanism to control transactions.
2. Conditions for the effectiveness of the communication mechanism.
With the spring transaction propagation mechanism, what are the conditions for the existence of this mechanism? We know that spring transactions are based on aop, or rather, AOP based on JDK dynamic agents. What are the characteristics of this AOP? It is based on a class or interface, that is, when @ Transactional is written on a method, the method will be dynamically proxied by spring to generate a dynamic proxy class that modifies and enhances the original method, but be careful! The class of the original method is no different, and there is no transaction. The proxy class generated by the spring dynamic proxy class has transactions and enhancements, that is, when the transaction method of this class is called through this.xx () in the same class, the transaction will not take effect, because you are not calling the proxy class.
@ Transactional @ Override public void method1 () {this.method2 ();} @ Transactional (propagation = Propagation.REQUIRES_NEW) @ Override public void method2 () {xx} solution
The key is to get the proxy object of the class, not to call it through this, so the following methods are based on this key point.
1, the simplest, the two transaction methods are put in different service, this is relatively simple, do not give an example. (recommended)
2. AOP context. Spring provides the AOP context AopContext, so the proxy object can be easily obtained through AopContext.
Public class Myservice {@ Transactional @ Override public void method1 () {((Myservice) AopContext.currentProxy ()) .method2 ();} @ Transactional (propagation = Propagation.REQUIRES_NEW) @ Override public void method2 () {xx}}
Once running, an error was reported, because exposeProxydefaults to false. If we want to expose the proxy class, we need to set it to true. We can add a note to the springboot startup class.
@ EnableAspectJAutoProxy (exposeProxy = true)
3 、 ApplicationContext
Public class Myservice {@ Autowired ApplicationContext context; Myservice service; @ PostConstruct / / is called at initialization, or private void getMyservice () {service = context.getBean (Myservice.class);} @ Transactional @ Override public void method1 () {service.method2 ();} @ Transactional (propagation = Propagation.REQUIRES_NEW) @ Override public void method2 () {xx}}
The difference between the second and the third is that 2 gets the proxy class directly and 3 gets the proxy class indirectly by calling getBean. Generally speaking, the first is the most convenient and recommended approach.
3. The type of communication mechanism
The following types are all for the called method, which is understood as a call to two service methods.
PROPAGATION_REQUIRED (default)
Supports the current transaction. If there is no current transaction, create a new transaction
If there is a current transaction, join the current transaction and merge into one transaction
REQUIRES_NEW (usually used in submethods that require separate transactions)
Create a new transaction and suspend the current transaction if it exists
This method commits the transaction independently, not affected by the caller's transaction, the parent exception, and it commits normally.
(the above two types are commonly used, while the following ones are rarely used.)
NESTED
If a transaction exists, it will become a child transaction of the parent transaction. The method will not be committed after the method ends, but will not be committed until the parent transaction finishes.
If there is no transaction currently, create a new transaction
If it is abnormal, the parent can catch its exception without rollback and commit normally
But if the parent is abnormal, it must be rolled back, which is the difference from REQUIRES_NEW
SUPPORTS
If a transaction currently exists, join the transaction
If there is no transaction at present, it runs in a non-transactional manner, which is no different from not writing
NOT_SUPPORTED
Run in a non-transactional manner
If there is a current transaction, suspend the current transaction
MANDATORY
If there is a current transaction, run in the current transaction
If there is currently no transaction, an exception is thrown, that is, the parent method must have a transaction
NEVER
Run in a non-transactional manner, and if a transaction currently exists, an exception is thrown, that is, the parent method must have no transaction
At this point, I believe you have a deeper understanding of the "detailed introduction to the transaction communication mechanism of spring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.