我有一个方法 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 transaction attribute is set to the REQUIRED value with a valid transaction context.
If a client invokes the enterprise bean’s method while the client is associated with a transaction context, the container invokes the enterprise bean’s method in the client’s transaction context.
If the client invokes the enterprise bean’s method while the client is not associated with a transaction context, the container automatically starts a new transaction before delegating a method call to the enterprise bean business method. The container automatically enlists all the resource managers accessed by the business method with the transaction. If the business method invokes other enterprise beans, the container passes the transaction context with the invocation. The container attempts to commit the transaction when the business method has completed. The container performs the commit protocol before the method result is sent to the client.
关于java - 如果外部方法失败,我希望我的子方法不被执行(使用 javax @TransactionAttribute),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52780786/