知识卡片
仓储不自行提交事务
仓储负责把对象变更加入当前数据库会话,不应在每次 add、update 时自行提交。一次业务操作可能横跨多个仓储,事务边界应由调用上下文或工作单元统一决定,才能一起提交或回滚数据库变更。外部服务调用不受本地数据库回滚保护,需另行设计跨服务一致性。参见 [[跨服务事务需要协调]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Microservice APIs》第 7 章《Service implementation patterns for microservices》(源文件:_epub-src/OEBPS/Text/07.htm)
- 结论依据:原文要求仓储将对象加入会话但不提交,将事务提交或回滚交由调用上下文;书中又把外部支付/厨房调用描述为一起成败,这不能仅由本地数据库事务保证,故卡片限定为本地事务边界。
- 原始内容:when we add an order object to the repository, the repository will add the order to a database session, but it will not commit the changes. Instead, it will be the responsibility of the consumer of OrdersService (i.e., the API layer) to commit the changes.