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

标签 java transactions ejb

我有一个方法 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/

相关文章:

java - 如何从本地docker容器中运行的rabbitmq队列读取消息

java - 使用线程时程序执行成功但程序执行并未结束

php - MongoDB 4.0 中使用 PHP 的事务示例

java - GlassFish 4 (EJB3) - 无状态 bean 生命周期

java - 在一个 jframe 中的两个 jpanel 中使用两种绘制方法

java - SQL Server 2012 上的 "New request is not allowed to start because it should come with valid transaction descriptor"

transactions - 为什么这个自定义程序被命名为AQZZ...?

mysql - Grails 立即提交事务中的对象

java - 拦截器API : in a classic java project

java - 如何在多个应用程序之间共享业务逻辑