javajpaejbstateless-session-bean

can transaction span multiple business methods with stateless + transaction scoped persistence context


given that the default persistence context for EJB (including stateless) is TRANSACTION_SCOPED as I know, is the statement 'transaction can span multiple business methods with stateless bean' valid even if the persistence context is defaulted to TRANSACTION_SCOPED or it's only specific to the popular use case of stateless bean persistence context (i.e. EXTENDED)

An answer supported with a reference is highly appreciated

EDIT:

the use case that I am asking about is something like that :

@Stateless
TransactionManagement(TransactionManagementType.BEAN)
public class MyStatelessBean(){
  @PersistenceContext(unitName="pU",type=PersistenceContextType.TRANSACTION) //default
  @Resource UserTransaction tx;

  public method1(){
    tx.begin();
    //bla bla bla
  }

  public method2(){
    tx.commit();
  }
}

and in the client:

callerMethod(){
   myStatelessBean.method1();
   myStatelessBean.method2();
}

will the transaction remain alive with no issues after returning from method1() and then can be committed in a separate call from the client to another method method2() ? and what can be a use case for such scenario ?


Solution

  • EDIT: based on the change of the original post. Since you are using BEAN managed transaction you take reposnsibility for the transaction and its scope. You have two cases:

    You use a remote client , if remote will not have transaction UserTransaction associate so the two methods will be executed in two different contexts. Since your transaction is BEAN managed the container will not take over. It is a Stateless Session Bean so you don't have guarantees that the second method will be invoked on the exact same instance of the Stateless bean. So you see why the answer is NO.

    Your client is actualy another EJB. As long as you reuse the same transaction manager which is the case when you use Hibernate for example the two methods will participate in the same transaction.

    Hi both TRANSACTION_SCOPED and EXTENDED scoped persistence contexts can span over several business methods. In the case of TRANSACTION_SCOPED persistence context the context is limited within the boundaries of the transaction. But there is something that is called TRANSACTION PROPAGATION and one transaction can span over multiple business methods. This is the reason why in the documentation it is stated that it can span over multiple business methods.

    Here is a link of transaction propagation explained

    The main difference between TRANSACTION_SCOPED and EXTENDED is that EXTENDED context can span over multiple trasactions and this is why it is always bound to Stateful session bean. Once the Statefull session bean is removed, the persistence context is closed.

    A very bried example of transaction propagation is:

    @EJB class A {

       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public methodA
    
    }
    
    @EJB
    class B {
    
       @TransactionAttribute(TransactionAttributeType.REQUIRED)
       public methodB()
    
    }
    

    In this case since methodA has required attribute and methodB has required attribute a call of B from A the transaction context will be propagated. The key point here is that if both EJB A as access to EntityManager and EJB B has access to EntityManager the session will be the same for both EJBs within the same transaction.

    Now if we switch methodB declaration to be @TransactionAttribute(TransactionAttributeType.NEW) instead a new transaction will be created in paralel to transaction initiated in methodA

    The default value when no @TransactionAttribute is defined is REQUIRED. As such in the normal case all transactions are propagated.