gpt4 book ai didi

java - @Transactional 的 "REQUIRES_NEW"传播属性的实际用例是什么

转载 作者:行者123 更新时间:2023-11-30 05:28:55 26 4
gpt4 key购买 nike

我是学习 Spring Boot 的新手。我想知道 @Transactional 注释的传播属性中 REQUIRES_NEW 的实际用例是什么。

我知道,如果新事务的请求到达,它会暂停正在进行的事务,然后在完成新请求的事务后恢复。

最佳答案

我认为Understanding transaction pitfalls文章以很好的方式解释了它:

the REQUIRES_NEW transaction attribute should be used only if the database action in the method being invoked needs to be saved to the database regardless of the outcome of the overlaying transaction. For example, suppose that every stock trade that was attempted had to be recorded in an audit database. This information needs to be persisted whether or not the trade failed because of validation errors, insufficient funds, or some other reason. If you did not use the REQUIRES_NEW attribute on the audit method, the audit record would be rolled back along with the attempted trade. Using the REQUIRES_NEW attribute guarantees that the audit data is saved regardless of the initial transaction's outcome.

关于java - @Transactional 的 "REQUIRES_NEW"传播属性的实际用例是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57967053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com