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 有什么问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)