首页 > 解决方案 > EJB3 事务 REQUIRES_NEW 未提交

问题描述

我在 EJB3/JTA 事务和事务隔离方面遇到了一些问题。

我正在使用 WildFly 17。

我有一个无状态 EJB 方法,它从另一个 EJB 调用不同的方法。

我希望使用 REQUIRES_NEW 每个方法都有自己的事务,并且每个方法都会提交到数据库。

所以下一个方法可以查看和使用之前插入或更新的数据库中的数据。

但事实并非如此。在 test3() 中,我看不到/使用来自 test1() 和 test2() 的数据。

我已经阅读了许多有关此行为的其他问题和答案,但没有一个帮助我找到解决此问题的方法。

这是我的问题的伪代码。

@Stateless
public class A {
    @Inject IAnotherEJB service;

    public test() {
        service.test1();
        //here, in the "main method" i can see X from the database
        service.test2();
        //here, in the "main method" i can see X and Z from the database
        service.test3();
    }
}


@Stateless
public class AnotherEJB implements IAnotherEJB {
    
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public test1() {
        ... 
        //add X and update Y to the database (JPA)
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public test2() {
        ...
        //add/update other some data Z to the database (JPA)
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public test3() {
        ... 
        //find X and Z from the database (JPA finById)
        //Here i can't see X or Z in the database or i have some error : Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) about Y
        ... 
        //add/update other some data W to the database (JPA) using X and Z
    }
}

像这样使用 REQUIRES_NEW 有什么问题吗?

标签: jpajakarta-eeejb-3.0jta

解决方案


推荐阅读