春季需要 PROPAGATION_NESTED 与 PROPAGATION_Required 吗?

发布于 2024-11-23 15:26:34 字数 358 浏览 1 评论 0原文

实际上寻找 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

開玄 2024-11-30 15:26:34

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.

蓝眼泪 2024-11-30 15:26:34

因为您使用 Spring 进行必需/嵌套传播,所以问题中提到的 [Transaction1] 和 [Transaction2] 是“相同的事务”。

作为您的用例,如果您在 method2() 上使用“必需”

@Transaction(Require)
main() {
    // throw new Exception(); => rollback all
    method1();
    method2();
    // throw new Exception(); => rollback all
}

@Transaction(Require)
method1() {
    // throw new Exception(); => rollback all
}

@Transaction(Require)
method2() {
    // throw new Exception(); => rollback all
}

如果您在 method2() 上使用

@Transaction(Require)
main() {
    // throw new Exception(); => rollback all
    method1();

    // Create Save Point A
    method2();
    // Release Save Point A

    // throw new Exception(); => rollback all
}

@Transaction(Require)
method1() {
    // throw new Exception(); => rollback all
}

@Transaction(Nested) // is the same transaction as main
method2() {
    // throw new Exception(); => will only rollback to Save Point A
}

嵌套 嵌套事务的用例

(当一个客户需要一百万个帐户,并且需要几个小时才能完成所有任务)

收益 = >

  1. 一个帐户创建失败不会全部回滚(只回滚失败的帐户=>然后可以开始调试)[当公司有午夜批次处理大量独立数据时节省时间]

  2. 它们都在同一个连接/事务中[与需求相比节省资源new]

例如:

@Transaction(Require)
main() {
    // throw new Exception(); => rollback all
    method1();

    for(many time) {
        // Create Save Point
        method2();
        // Release Save Point
    }

    // throw new Exception(); => rollback all (Be careful, it will rollback all!!!)
}

@Transaction(Require)
method1() {
    // throw new Exception(); => rollback all
}

@Transaction(Nested) // is the same transaction as main
method2() {
    // throw new Exception(); => will only rollback to Save Point
}

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()

@Transaction(Require)
main() {
    // throw new Exception(); => rollback all
    method1();
    method2();
    // throw new Exception(); => rollback all
}

@Transaction(Require)
method1() {
    // throw new Exception(); => rollback all
}

@Transaction(Require)
method2() {
    // throw new Exception(); => rollback all
}

If you use nested on method2()

@Transaction(Require)
main() {
    // throw new Exception(); => rollback all
    method1();

    // Create Save Point A
    method2();
    // Release Save Point A

    // throw new Exception(); => rollback all
}

@Transaction(Require)
method1() {
    // throw new Exception(); => rollback all
}

@Transaction(Nested) // is the same transaction as main
method2() {
    // throw new Exception(); => will only rollback to Save Point A
}

Nested Transaction's Use Case

(when one customer need one million account, and it need couple of hour to complete all the task)

Benefit =>

  1. 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]

  2. They are all in the same connection/transaction [save resource compare to require new]

ex:

@Transaction(Require)
main() {
    // throw new Exception(); => rollback all
    method1();

    for(many time) {
        // Create Save Point
        method2();
        // Release Save Point
    }

    // throw new Exception(); => rollback all (Be careful, it will rollback all!!!)
}

@Transaction(Require)
method1() {
    // throw new Exception(); => rollback all
}

@Transaction(Nested) // is the same transaction as main
method2() {
    // throw new Exception(); => will only rollback to Save Point
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文