gpt4 book ai didi

java - 如果外部方法失败,我希望我的子方法不被执行(使用 javax @TransactionAttribute)

转载 作者:行者123 更新时间:2023-12-02 10:43:13 25 4
gpt4 key购买 nike

我有一个方法 Meth1,女巫会从另一个类调用 Meth2。我希望如果 Meth1 失败,则不执行 Meth2。

它们都有 REQUIRED 属性。使用此代码,如果 Meth1 在持久化过程中失败(Meth2 成功执行后),Meth2 的更改将保留。

我希望所有代码都作为整个事务执行,要么全有,要么全无。

例如:

@TransactionAttribute(TransactionAttributeType.REQUIRED)
void meth1(){
// ...some code
otherClass.meth2();
// ... some persistance that may fail
}

@TransactionAttribute(TransactionAttributeType.REQUIRED)
void meth2(){
// some persistance
}

谢谢!

最佳答案

在这种情况下,这两个方法将在同一事务中执行。

EJB 规范 3.0 讲述了 REQUIRED:

The container must invoke an enterprise bean method whose transactionattribute is set to the REQUIRED value with a valid transactioncontext.

If a client invokes the enterprise bean’s method while theclient is associated with a transaction context, the container invokesthe enterprise bean’s method in the client’s transaction context.

Ifthe client invokes the enterprise bean’s method while the client isnot associated with a transaction context, the container automaticallystarts a new transaction before delegating a method call to theenterprise bean business method. The container automatically enlistsall the resource managers accessed by the business method with thetransaction. If the business method invokes other enterprise beans,the container passes the transaction context with the invocation. Thecontainer attempts to commit the transaction when the business methodhas completed. The container performs the commit protocol before themethod result is sent to the client.

关于java - 如果外部方法失败,我希望我的子方法不被执行(使用 javax @TransactionAttribute),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52780786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com