qtm
qtm

Reputation: 93

EJB3.1 - Transactions over multiple applications in the same JVM

I've got the following situation:

Let's say in App2 in a service there is a method:

void method(){
   doDBWorkinApp2();
   callApp1ServiceToDoMoreDBWork();
   moreDBWorkinApp2();
}

I want this method to be executed in a single transaction. How can I do that?

Upvotes: 2

Views: 918

Answers (1)

Sergio
Sergio

Reputation: 3387

Well assuming stateless, the transaction/persistence scope is in general propagated. But, take into the account the transaction attribute that you use for each method (i.e NOT_SUPPORTED, SUPPORTS, MANDATORY..) and some rules of propagation(the rules: https://weblogs.java.net/blog/ss141213/archive/2006/10/persistence_con.html).

If a transaction-scoped entity manager is invoked from within a transaction, a new persistence context is created if there isn't one already and associated with the transaction (Enterprise JavaBeans 3.0)

So, if you are using the default Tx attribute (REQUIRED) for the two stateless: They will share the same context, so, if 1 method fails all gets roll backed. If you dont want this behaviour you can still use something like (REQUIRES_NEW , NOT_SUPPORTED..)

To know more about transaction attributes: http://docs.oracle.com/javaee/6/api/javax/ejb/TransactionAttributeType.html

EDIT: In the case of multiple EARs each EJB has a context (kind of client context) and will be propagated according to the TransactionAttribute of the EJB invoked, this is true only for local calls. Note that, you can't share the persistence context across remote EJB calls.

Upvotes: 4

Related Questions