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 use transactions in SpringBoot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to use transactions in SpringBoot, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.

Use of transactions in Springboot:

1. Start the class with @EnableTransactionManagement annotation to enable transaction support (actually enabled by default).

Add the @Transactional annotation to public (only public supports transactions) methods that use transactions (or classes-equivalent to all public methods of that class).

In actual use,@Transactional is usually used in service, so for controller->service flow:

If the controller does not open the transaction, the transaction starts in the service, the service is successfully executed, and the controller has an exception (error) in the subsequent operation, and it will not be automatically rolled back.

That is, only exceptions in the method that opened the transaction (default only non-detectable exceptions take effect-RuntimeException)(error-Error) are automatically rolled back.

If you want to automatically roll back any exception thrown (not just RuntimeException), you can simply use @Transactional(rollbackFor = Exception.class).

Transaction rollback in the method that started the transaction:

① Unfound exception, RuntimeException or its subclass is automatically thrown during program running, program termination, automatic rollback.

② Manual rollback using TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();.

③ Note: If the possible exception (RuntimeException) is handled in the try-catch statement and there is no manual throw exception, spring considers that the method is successfully executed and will not roll back. At this time, you need to call the method in ② to manually roll back (java framework project case: www.1b23.com)

In addition, if the try-catch statement returns in finally, the exception thrown manually in catch is overridden and does not roll back automatically.

//does not automatically roll back try{ throw new RuntimeException();}catch(RuntimeException e){ e.printStackTrace();}finally{}//automatically rolls back try{ throw new RuntimeException();}catch(RuntimeException e){ e.printStackTrace(); throw new RuntimeException();}finally{} Is it helpful to read the above? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report