Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement exception rollback transaction in SpringBoot

Shulou Source: shulou.com Published: 2022-06-02 07:30:03 12月04日 Update

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!

Tags: Transactions methods manual only success content more comments programs statements errors problems support running helpless for this that is to say the reason the actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info vpn Redmi Apple