使用Spring和Hibernate嵌套事务

使用Spring和Hibernate嵌套事务

本文介绍了使用Spring和Hibernate嵌套事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,有多个步骤,许多提交到数据库的步骤将通过多个方法按顺序进行。
示例:

  A  - > B  - > C 
- > D
- > E
- > F
- > G

呼叫B呼叫C.然后B呼叫D. D呼叫E等等。所有这些方法都有一些数据库操作。
根据我对 PROPAGATION_REQUIRED (声明式事务管理 - 春季推荐的方式)的理解,如果 E 成功完成,则事务(和操作在 E 将被提交)。现在,由于某些例外情况, F 应导致回滚。我希望所有内容都从 A 开始回滚。
这是可能通过声明式事务管理?或者我应该使用程序化交易管理?



谢谢。

解决方案

首先,嵌套事务,就是说有多个依赖于对方的正在运行的事务,不受支持,afaik。

然后, propagation = REQUIRED 意味着所有使用该传播的方法将会:


  • 如果没有存在

  • 如果存在,则参与现有交易。



这意味着在您的场景中, 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.

解决方案

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:

  • start a new transaction if there is none exists
  • participate in an existing transaction if such exists.

This means that in your scenario, a failure in F would rollback the entire transaction (because it is a single transaction, started by A, and propagated to other methods)

这篇关于使用Spring和Hibernate嵌套事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:59