2

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 ?

osama yaccoub
  • 1,884
  • 2
  • 17
  • 47

1 Answers1

1

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.

Alexander Petrov
  • 9,204
  • 31
  • 70
  • thank you for your answer, actually I was not asking about propagation ... I meant a certain scenario that I mentioned in the edit, if you can help with this I will be thankful – osama yaccoub Mar 08 '18 at 08:43
  • @osamayaccoub when you say Client in the Edit do you mean another EJB, or do you mean a remote client. If it is a remote client then the transaction will not span over the different business methods wi. IF it is another EJB that is the client then the transaction context will be propagated. – Alexander Petrov Mar 08 '18 at 08:58
  • thanks Alex, but actually I am not concerned much with propagation ... I know that no propagation with remote client .... I am rather concerned if the transaction will outlive the method reply then call again ... will the transaction still be open when I call method2() .... i.e. is it allowed to open the transaction in one business method (method1()) and close it in another (method2()) with the above configuration given that the method2() will not be called inside method1() but rather in a separate call from the client ? – osama yaccoub Mar 08 '18 at 10:02
  • @osamayaccoub The two things are related. Read again what I have written in the EDIT part of the answer. If you have remote client it will not outlive it., If you have EJB as a client than you can have the two methods in the same transaction. This I have already described in the EDIT in the answer. – Alexander Petrov Mar 08 '18 at 11:04