春季需要 PROPAGATION_NESTED 与 PROPAGATION_Required 吗?
实际上寻找 PROPAGATION_NESTED(如果当前事务存在,则在嵌套事务中执行)和 PROPAGATION_Required(支持当前事务)之间的区别。
下面是简单的用例
,在主类中我们调用 method1 并使用 jdbc[Transaction1] 创建客户。尚未提交。现在我们调用主类中的方法2并为刚刚创建的客户[Transaction2]创建帐户。现在提交它。我们可以将事务 2 称为嵌套事务吗?
根据我现在的理解,如果我们将事务定义定义为 PROPAGATION_NESTED,
事务 2 将被视为嵌套事务,但如果我们将其定义为 PROPAGATION_Required 它将支持当前事务。那么嵌套和必需之间有什么区别?
Actually looking for difference between PROPAGATION_NESTED( Execute within a nested transaction if a current transaction exists) and PROPAGATION_Required(Support a current transaction).
Below is simple usecase
say in main class we call method1 and create the customer using jdbc[Transaction1]. Not commited yet.now we call the method2 in main class and create the account for the just created customer[Transaction2]. now commit it. can we call transactions 2 as nested transaction.
As per my understanding now if we define the transaction definition as PROPAGATION_NESTED
transaction 2 will be treated as nested but if we define it as PROPAGATION_Required it will support the current transaction. so whats the differnce between nested and required?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PROPAGATION_NESTED 只能与 DataSourceTransactionManager 和 JDBC3 驱动程序一起使用。它使用保存点以便能够回滚事务的某些部分(即 Spring 术语中的嵌套事务的构成部分)。请参阅 Connection 的 javadoc 了解如何保存点工作。
REQUIRED 完全不同。它只是意味着:如果已经存在一个事务,则执行该事务中的工作;否则,启动一个新事务,完成工作并提交事务。
PROPAGATION_NESTED can only be used with a
DataSourceTransactionManager
and a JDBC3 driver. It uses save points in order to be able to rollback some part of a transaction (i.e. what constitutes the nested transaction in Spring terms). See the javadoc of Connection to see how save points work.REQUIRED is completely different. It just means: if there is a transaction already existing, do the work in this transaction; else, start a new transaction, do the work, and commit the transaction.
因为您使用 Spring 进行必需/嵌套传播,所以问题中提到的 [Transaction1] 和 [Transaction2] 是“相同的事务”。
作为您的用例,如果您在 method2() 上使用“必需”
如果您在 method2() 上使用
嵌套 嵌套事务的用例
(当一个客户需要一百万个帐户,并且需要几个小时才能完成所有任务)
收益 = >
一个帐户创建失败不会全部回滚(只回滚失败的帐户=>然后可以开始调试)[当公司有午夜批次处理大量独立数据时节省时间]
它们都在同一个连接/事务中[与需求相比节省资源new]
例如:
Because you are using Spring with required/nested propagation, your [Transaction1] and [Transaction2] mentioned in the question are "The Same Transcation".
As your Use Case, if you use "required" on method2()
If you use nested on method2()
Nested Transaction's Use Case
(when one customer need one million account, and it need couple of hour to complete all the task)
Benefit =>
One account create failure won't rollback all (only rollback the failure account => you can then start debug) [save time when company have midnight batch that deal with lots of independent data]
They are all in the same connection/transaction [save resource compare to require new]
ex: