使用 Spring 和 Hibernate 的嵌套事务
在我的应用程序中,有多个步骤,其中许多提交将通过多种方法按顺序进行。 示例:
A -> B -> C
-> D
->E
-> F
-> G
A 调用 B,B 调用 C。然后 B 调用 D。D 调用 E,依此类推。所有这些方法都有一些数据库操作。 据我从 PROPAGATION_REQUIRED
(声明式事务管理 - spring 推荐的方式)中了解到,如果 E 成功完成,事务(以及 E 中的操作)将承诺)。现在,由于某些异常,F 应该会导致回滚。我希望从 A 所做的事情开始回滚一切。 这可以通过声明式事务管理实现吗?或者我应该使用程序化事务管理?
谢谢。
In my app, there are multiple steps where many commits to the database will be made sequentially through multiple methods.
Example:
A -> B -> C
-> D
->E
-> F
-> G
A calls B which calls C. Then B calls D. D calls E and so on. All of these methods have some database operations.
As I understand from PROPAGATION_REQUIRED
(declarative transaction management - the spring recommended way), if E completes successfully, the transaction (and operations in E will be committed). Now, due to some exception, F should lead to a rollback. I want to have everything rolled-back starting from what A did.
Is this possible via Declarative Transaction management? Or should I use Programmatic Transaction Management?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,据我所知,不支持“嵌套”事务,即存在多个相互依赖的正在运行的事务。
然后,propagation=REQUIRED 意味着具有该传播的所有方法都将:
这意味着在您的场景中,
F
中的失败将回滚整个事务(因为它是单个事务,由A
启动,并传播到其他方法)First, "nested" transactions, in the sense that there are multiple running transactions depending on each other, is not supported, afaik.
Then,
propagation=REQUIRED
means that all methods with that propagation will:This means that in your scenario, a failure in
F
would rollback the entire transaction (because it is a single transaction, started byA
, and propagated to other methods)