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

标签 java spring spring-boot transactions atomic

我也有一个调用其他方法的方法。这些操作与数据库没有任何关系。我只是在执行一些逻辑,对其他一些外部服务和所有其他服务进行少量 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/

相关文章:

java - Zxing扫描停止

java - JNI GetObjectClass 始终返回 java/lang/Class

spring - 如何在 Drools 规则中使用 Spring 服务?

css - JSP找不到css文件(基于java的webconfig)

java - spring中如何初始化自定义注解

docker - 在 kubernetes 中部署启用 https 的 Spring Boot Rest 服务

java - 在 Spring 中排除单元测试 @Configurations 的最佳方法?

java - IDE 中的 Release模式与 Debug模式

java - 有没有办法确定 Elastic Beanstalk 上 Tomcat 环境的 URL 是什么?

spring - 运行 Spring 测试 : JdbcSQLSyntaxErrorException: Column "start_value" not found 时如何修复 H2 插件(版本 1.4.200)的错误