EJB 3.0 transaction boundary invoking one local EJB from another

Imagine the following two sessions without saving the state of ejb3.0 beans, each of them implements a local interface and deploys into the same container:

public class EjbA {
    @EJB 
    private ejbB;

    public void methodA() {
        for (int i=0; i<100; i++) {
            ejbB.methodB();
        }
    }    
}

public class EjbB {
    public void methodB() {
        ...
    }
}

When methodA is called, every call to method B triggers the start and commit of a new transaction? Or, since these are local beans, is there one transaction that starts when method A is called and reused by method B?

Hooray!

+3
source share
2 answers

It depends on your transaction attribute, which you can set using the @TransactionAttribute annotation to one of:

  • REQUIRED
  • REQUIRES_NEW
  • SUPPORT
  • MANDATORY
  • NOT_SUPPORTED

REQUIRED , , .

REQUIRES_NEW , .

, EJB.

:

@Stateless
public class EjbA {
    @EJB 
    private ejbB;

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void methodA() {
        for (int i=0; i<100; i++) {
            ejbB.methodB();
        }
    }    
}

... A() .

+2

, .

B A:

A B: B

+1

Source: https://habr.com/ru/post/1762465/


All Articles