gpt4 book ai didi

java - 如何确保 spring boot 中方法执行的原子性?

转载 作者:行者123 更新时间:2023-12-04 13:13:40 25 4
gpt4 key购买 nike

我也有一个调用其他方法的方法。这些操作与数据库没有任何关系。我只是在执行一些逻辑,对其他一些外部服务和所有其他服务进行少量 API 调用。如果在任何操作中发生任何异常,我希望所有操作都必须回滚。我怎样才能做到这一点?

最佳答案

很抱歉,您必须手动处理它。例如,如果发生异常,手动应用一个补偿 Action 来撤销。

saga模式就是用来解决这类问题的,我引用它的基本思想this如下:

The Saga pattern describes how to solve distributed (business) transactions without two-phase-commit as this does not scale in distributed systems. The basic idea is to break the overall transaction into multiple steps or activities. Only the steps internally can be performed in atomic transactions but the overall consistency is taken care of by the Saga. The Saga has the responsibility to either get the overall business transaction completed or to leave the system in a known termination state. So in case of errors a business rollback procedure is applied which occurs by calling compensation steps or activities in reverse order.

关于java - 如何确保 spring boot 中方法执行的原子性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62255168/

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