In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to understand java spring nested transactions and transaction propagation types". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand java spring nested transactions and transaction propagation types.
What happens when a transactional method invokes another transactional method?
What if a method without a transaction invokes a method with a transaction?
Answers to multi-transaction nesting problems
1. In the same class, a non-transactional method calls another method with transaction annotations (such as @ Async,@Transational), and annotated transactions will not take effect.
2. In the same class, one transactional method calls another method with transactional annotations (such as @ Async,@Transational). Only the outer transaction is valid, and the annotated transaction will not take effect.
3. In different classes, one non-transactional method calls another method with transaction annotations (such as @ Async,@Transational), and annotated transactions take effect.
4. In different classes, one method with transaction calls and the other method with transaction annotation (such as @ Async,@Transational). Both methods have transactions.
Code example:
@ Service
Public class PersonServiceImpl implements PersonService {
@ Autowired
PersonDao personDao
@ Override
@ Transactional
Public boolean addPerson (Person person) {
Boolean result = personDao.insertPerson (person) > 0? True: false
Return result
}
@ Override
@ Transactional
Public boolean updatePersonByPhoneNo (Person person) {
Boolean result = personDao.updatePersonByPhoneNo (person) > 0? True: false
AddPerson (person); / / Test whether @ Transactional works in the same class
Return result
}
}
In the above case, to put it bluntly, within an Service, nested calls between transaction methods, regardless of normal methods and transaction methods being called, will not open a new transaction! Which Zhengzhou abortion hospital has a good http://www.gz020zj.com/?
When spring scans the bean, it scans for @ Transactional annotations on the method. If so, spring dynamically generates a class (that is, the proxy class, proxy) for the bean, which inherits the original bean. At this point, when the annotated method is called, it is actually called by the proxy class, which starts the transaction before it is called. However, if the annotated method is called by another method in the same class, then the method is called not through the proxy class, but directly through the original bean, so the transaction will not be started, and what we see is that the @ Transactional annotation is invalid.
Summary: spring uses dynamic proxy mechanism to achieve transaction control, dynamic proxy will eventually call the original object, and the original object will not trigger the proxy when it invokes the method!
Important: you can also implement a transaction by yourself, which is based on the dynamic proxy in AOP.
The nested transaction solution is simple (two):
Separate the two methods into different classes and add comments to the class name
7 propagation attributes of Spring transaction:
REQUIRED (require) default level: create a transaction if there is no transaction
REQUIRES_NEW (requires_new): create a new transaction of your own, regardless of whether the current transaction exists or not, the existing transaction is suspended
NESTED (nested): if a transaction exists, it runs in a nested transaction, and a transaction is created whether the caller or callee does not exist
SUPPORTS (supports): join if there is a transaction, or you can join if it doesn't exist
NOT_SUPPORT (not_support): runs in a non-transactional manner, suspending the current transaction if there is a current transaction
MANDATORY (mandatory): if there is a transaction, it runs in the current transaction, and if there is no transaction, an exception is thrown, that is, the parent method must have a transaction
NEVER (never): the current method cannot be run in a transaction, and an exception is thrown if there is a transaction
Implement in the code:
@ Override
@ Transactional (propagation = Propagation.REQUIRED)
Public void addEmpByRequired (String name) {
Employee employee = new Employee ()
Employee.setDeptId (1)
Employee.setName (name)
Employee.setAddress (Handan)
EmployeeMapper.insertSelective (employee)
DepartmentService.addDept ("jishubu")
Int I = 1max 0
}
At this point, I believe you have a deeper understanding of "how to understand java spring nested transactions and transaction propagation types". 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.