In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to solve the problem of Spring annotations and synchronization locks can not be synchronized", 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 solve the problem that Spring annotations and synchronization locks can't be synchronized.
Conclusion: data synchronization cannot be guaranteed if both transaction and synchronization locks are used on the methods of the service layer.
@ Servicepublic class ServiceImpl {private static Lock lock = new ReentrantLock (false); @ Transactional (rollbackFor = Exception.class) public void update () {try {lock.lock ();...} catch (Exception e) {e.printStackTrace ();} finally {lock.unlock ();}
The above example does not guarantee the consistency of the data, the same goes for synchronized.
Reason:
According to the AOP nature of spring, the transaction is opened before the update method, then locked, and the transaction is committed when the locked code is finished.
Because lock code block execution is executed within a transaction, the transaction is not committed when the code block is finished, so after other threads enter the synchronized code block, the database data read is not up-to-date (dirty read).
Solution:
1. Add a synchronization lock before the transaction is opened, and use the locking method to call the transaction method.
@ Servicepublic class ServiceImpl {private static Lock lock = new ReentrantLock (false); public void update1 () {try {lock.lock (); update2 ();} catch (Exception e) {e.printStackTrace ();} finally {lock.unlock ();}} @ Transactional (rollbackFor = Exception.class) public void uodate2 () {...}}
two。 Put the lock on the upper floor
@ Controllerpublic class TestController {@ Autowired private IServiceImpl serviceImpl; private static Lock lock = new ReentrantLock (false); public String test () {try {lock.lock (); serviceImpl.update ();} catch (Exception e) {e.printStackTrace ();} finally {lock.unlock ();} @ Servicepublic class ServiceImpl {@ Transactional (rollbackFor = Exception.class) public void update () {...}}
At this point, I believe you have a deeper understanding of "how to solve the problem that Spring annotations and synchronization locks cannot be synchronized". 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.