In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people have no idea about how to implement abnormal rollback transactions in SpringBoot. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Use of transactions in Springboot:
1. Add @ EnableTransactionManagement annotation to the startup class to enable transaction support (in fact, it is enabled by default).
2. Add the @ Transactional annotation to the public method that uses the transaction (only public supports transactions) (or class-equivalent to all public methods of that class).
In practice, @ Transactional is generally used in service, so in the controller- > service process:
If the transaction is not opened in controller, the transaction starts in service, the service executes successfully, and an exception (error) occurs in the subsequent operation of controller, it will not be rolled back automatically.
That is, only an exception occurs in the method that opens the transaction (by default, only undetected exceptions take effect-RuntimeException) (error-Error) will be rolled back automatically.
If you want to automatically roll back any exceptions thrown (not just for RuntimeException), you just need to use @ Transactional (rollbackFor = Exception.class).
The rollback of the transaction in the method that opens the transaction:
If the exception is not found by ①, the RuntimeException or its subclasses will be thrown automatically when the program is running, and the program will be terminated and rolled back automatically.
② uses TransactionAspectSupport.currentTransactionStatus (). SetRollbackOnly (); for manual rollback.
③ Note: if the possible exception (RuntimeException) is handled in the try-catch statement and there is no manual throw exception, spring thinks that the method is executed successfully and will not be rolled back, so you need to call the method in ② for manual rollback (java framework project case: www.1b23.com)
In addition, if the try-catch statement does a return operation in finally, the exception manually thrown in catch will also be overwritten and will not be rolled back automatically.
/ / will not automatically roll back try {throw new RuntimeException ();} catch (RuntimeException e) {e.printStackTrace ();} finally {} / / will automatically roll back try {throw new RuntimeException ();} catch (RuntimeException e) {e.printStackTrace (); throw new RuntimeException ();} finally {} after reading the above, have you mastered how to realize abnormal rollback transactions in SpringBoot? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.