如果外部方法失败,我希望我的子方法不被执行(使用 javax @TransactionAttribute)
I want my child method to not be executed if the outside method will fail (using javax @TransactionAttribute)
我有一个方法 Meth1 将从另一个 class 调用 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.
我有一个方法 Meth1 将从另一个 class 调用 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.