传播需要澄清弹簧

发布于 2024-12-02 17:30:21 字数 1575 浏览 0 评论 0原文

想要澄清以下场景中传播所需的基本原理。 。请告诉我以下理解是否正确。

Class MyClass1{

//Propagation is Propagation required
public void method1();
{

method1A();
method1B();
MyClass2 myClass2= new MyClass2();
myClass2.method2A();
myClass2.method2B();
}


// No porapgation is defined here so default will be reuired
public method1A()
{
//Some Transaction
}


// No porapgation is defined here so default will be reuired
private method1B()
{
//Some Transaction
}

}



Class MyClass2{

//Propagation is Propagation required
public void method2()
{

method2A();
method2B();
}


// No porapgation is defined here so default will be required
public method2A()
{
//Some Transaction
}


// No porapgation is defined here so default will be required
public method2B()
{
//Some Transaction
}

}

场景

现在这是我们在主方法中调用 MyClass1 的 method1() 的

Scenarion1:-

没有发生异常。事务将在 method1A() 之前创建,并在 myClass2.method2B() 之后提交;

场景2:-

方法1B期间发生运行时异常。完整的事务将被回滚

场景3:-

方法2A期间发生运行时异常(方法2A下的事务将被视为创建事务的一部分 在class1中的method1下)。完整的事务将被回滚

场景4:-

在method2B期间发生运行时异常(method2A下的事务将被视为创建事务的一部分 在 class1 中的 method1 下)。完整的事务将回滚

编辑:-

现在,如果我们考虑与方法 method2A 和 method2B 的嵌套传播相同的场景。

场景1:-

没有发生异常。事务将在进入 method1A() 时创建,并在退出 method1A() 时提交

场景 2:-

在 method1B 期间发生运行时异常。完整的事务将被回滚

场景3:-

在method2A期间发生运行时异常。只有method2A下的事务将被回滚,其余事务将被提交 退出方法 1

场景 4:-

方法 2B 期间发生运行时异常。仅method2B下的事务将被回滚,其余事务将被提交 退出方法 1 时

wanted to clarify the Propagation required fundamental with below scenarios. . Please let me know if below understanding is correct.

Class MyClass1{

//Propagation is Propagation required
public void method1();
{

method1A();
method1B();
MyClass2 myClass2= new MyClass2();
myClass2.method2A();
myClass2.method2B();
}


// No porapgation is defined here so default will be reuired
public method1A()
{
//Some Transaction
}


// No porapgation is defined here so default will be reuired
private method1B()
{
//Some Transaction
}

}



Class MyClass2{

//Propagation is Propagation required
public void method2()
{

method2A();
method2B();
}


// No porapgation is defined here so default will be required
public method2A()
{
//Some Transaction
}


// No porapgation is defined here so default will be required
public method2B()
{
//Some Transaction
}

}

Now here are the scenarios

we call the method1() of MyClass1 inside main method

Scenarion1:-

No exception occurs. transaction will be created before method1A() and will be commited after myClass2.method2B();

Scenarion2:-

Runtime exception occurs during method1B. Complete transaction will be rolled back

Scenarion3:-

Runtime exception occurs during method2A(Transaction under method2A will be treated as part of transaction created
under method1 in class1) .Complete transaction will be rolled back

Scenarion4:-

Runtime exception occurs during method2B(Transaction under method2A will be treated as part of transaction created
under method1 in class1) .Complete transaction will be rolled back

Edit:-

Now if we consider the same scenarios with propagation as Nested for methods method2A and method2B.

Scenarion1:-

No exception occurs. transaction will be created on entering method1A() and will be commited on exit of method1A()

Scenarion2:-

Runtime exception occurs during method1B. Complete transaction will be rolled back

Scenarion3:-

Runtime exception occurs during method2A .Only transaction under method2A will be rolled back and rest of the transaction will be commited
on exit of method1

Scenarion4:-

Runtime exception occurs during method2B. Only transaction under method2B will be rolled back and rest of the transaction will be commited
on exit of method1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

长不大的小祸害 2024-12-09 17:30:21

您的理解(通常)是正确的,但您的示例有缺陷。通过调用:

MyClass2 myClass2= new MyClass2();

您已确保 myClass2 上的方法调用不会被事务代理拦截,因此此处隐含的任何所需的传播语义并不重要,因为它们不会被应用。然而,在这种情况下,您将落入 method1 的事务边界内,并且由于您已将其标记为需要传播,因此您的代码将按照您所描述的方式执行。如果您需要进一步说明,最好提供 SSCCE

另外,关于事务管理<的Spring文档/a> 是您能找到的最好的一些,我强烈建议您看一下。

Your understanding is (generally) correct, but your example is flawed. By calling:

MyClass2 myClass2= new MyClass2();

You've ensured that method calls on myClass2 will not be intercepted by the transactional proxy, and therefore any propagation required semantics implied here don't really matter since they won't be applied. In this case, however, you will fall within the transactional boundaries of method1 and since you've marked it as propagation required, your code will execute as you've described. You would do well to come up with a SSCCE if you require further clarification.

Also, the Spring documentation on Transaction Management is some of the best you'll find, I highly recommend you take a look at it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文