Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

jpa - EJB3 Transaction REQUIRES_NEW not committed

I'm having some troubles with EJB3/JTA transaction and transaction isolation.

I'm using WildFly 17.

I have a stateless EJB method that call different method from another EJB.

I was hopping that using REQUIRES_NEW each method will have it's own transaction, and each method will make a commit to the database.

So the next method can see and use data from the database inserted or updated before.

But it's not like this. in test3() i can't see/use data from test1() and test2().

I've read many other questions and answer about this behavior, but none helped me to find a solution to this problem.

Here is a pseudo code of my problem.

@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
    }
}

Is there something wrong using REQUIRES_NEW like this ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...