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

Can the transaction be rolled back correctly if the transaction takes effect?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

The main content of this article is to explain "can the transaction be rolled back correctly if it takes effect". 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 "can the transaction be rolled back correctly if the transaction takes effect?"

1 the principle of implementing transactions with AOP

It can be understood as the method of using try/catch to wrap an annotated @ Transactional:

When the method throws an exception and satisfies the condition, transaction rollback can be set in catch

If there is no exception, the transaction is committed directly.

The conditions mentioned just now are as follows:

The transaction can be rolled back only if the exception propagates the method annotated by @ Transactional.

The TransactionAspectSupport#invokeWithinTransaction method of Spring is the logic of dealing with transactions: only when an exception is caught can subsequent transactions be processed.

By default, transactions are rolled back only when RuntimeException or Error,Spring appears.

View the DefaultTransactionAttribute of Spring

The checked exception is usually the return value of a business exception or similar to another method. If such an exception occurs, the business may still be able to complete it, so it will not be rolled back actively.

Error or RuntimeException represent unexpected results and should be rolled back

2 negative teaching materials

2.1 registered user case

CreateUserError1 throws RuntimeException, but the catch in the method catches all exceptions

CreateUserError2, it will also readFile when registering a user. If reading the file fails, we expect the DB operation registered by the user to be rolled back. Although no exception is caught here, because readFile throws checked exception, createUserError2 propagates checked exception, and the transaction will not be rolled back.

ReadFile

Although createUserError1 and 2 methods can ensure that the transaction takes effect, the transaction will not be rolled back when the file operation is checked because of improper exception handling.

2.2 how to fix bug?

Use the log to verify that the repair is successful. In view of the above two situations, the repair schemes are as follows.

2.2.1 repair bug1

If you want to catch the exception and handle it yourself, you can manually set it so that the current transaction is in the rollback state.

Check the log to make sure the transaction is rolled back.

Transactional code has requested rollback: manually request a rollback.

2.2.2 repairing bug2

State in the comments that you expect all Exception encounters to roll back transactions.

In order to break the default limit that Spring does not roll back checked exceptions.

Check the log to confirm that the transaction has been rolled back:

In this case, there are not only DB operations but also IO operations, and the DB transaction is expected to be rolled back when IO encounters a problem to ensure logical consistency. Be careful not to step on the pit again.

3 Summary

Due to incorrect exception handling, it often causes the transaction to take effect, but it still fails to roll back correctly when an exception occurs.

By default, Spring rolls back only when RuntimeException and Error occur for methods annotated by @ Transactional, so if the method catches an exception, you need to handle the transaction rollback through handwritten code.

If you want Spring to be rolled back for other exceptions, you can configure the rollbackFor and noRollbackFor attributes of the @ Transactional annotation to override the default configuration of Spring.

At this point, I believe that everyone has a deeper understanding of "can the transaction be rolled back correctly if the transaction takes effect?" 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.

Share To

Database

Wechat

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

12
Report