Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that the spring method in the service layer reports an error and the transaction will not be rolled back?

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "spring in service layer method error reporting transaction will not be rolled back how to solve", the editor shows you the operation process through the actual case, the operation method is simple, fast and practical. I hope this article "spring method in service layer error reporting transaction will not be rolled back how to solve" article can help you solve the problem.

Spring will not roll back @ Transactional (rollbackFor = {Exception.class}) public void insertData () throws Exception {/ / business code 1 business1 (); / / business code 2 business2 (); / / business code 3 business3 () } public void business1 () {/ / omit business operations here} public void business2 () {/ / omit business operations here} public void business3 () {/ / omit business operations here}

When the above code executes the insertData method in the service layer, there is an error in the code execution in business3, and the operations in business1 and business2 are not rolled back

Solution method

Method 1: catch the exception in the service method and throw the runtime exception out at the same time

@ Transactional (rollbackFor = {Exception.class}) public void insertData () throws Exception {try {/ / Business Code 1 business1 (); / / Business Code 2 business2 (); / / Business Code 3 business3 ();} catch (Exception) {throw new RuntimeException (e);}}

Method 2: catch an exception in the service method and manually roll back the transaction

Service manual rollback issu

If the management of service layer things is configured for spring.

In some businesses, rollback is required. Normally, a runtime exception is thrown.

Throw new RuntimeException ()

Only in this way, the code ends. If you want to return an error message to the user, which is not convenient, you can add the following code to manually roll back in catch.

TransactionAspectSupport.currentTransactionStatus () .setRollbackOnly ()

This not only returns the error message normally, but also maintains the atomicity of things.

This is the end of the content about "how to solve the problem that the spring method in the service layer will not roll back the transaction". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report