I have a following situation:
- Client is calling Stateless local EJB - managed transaction begins with this invocation
- Local EJB builds InitialContext and looks up remote EJB
- Local EJB invokes method on remote EJB
- Local EJB closes context and connection to remote EJB
- Container tries to commit transaction
Distributed transaction cannot be committed because connection to remote EJB that was taking part in transaction cannot be contacted because connection to it is closed.
My question is: Is it possible to use remote EJB calls when there already is a transaction active? How should I close context that was used to look up remote EJB?
Following pseudo code illustrates my issue:
@Stateless
public class LocalEjb {
public void localEJBMethod() {
//transaction starts before this method execution
Context ctx = //create initial context
RemoteEjb remoteEjb = (RemoteEjb) ctx.lookup("jndi name");
remoteEjb.remoteMethod(); //remote EJB takes part in distributed transaction
ctx.close();
//error occurrs when container tries to commit distributed transaction after
//this method returns
}
}
public class ClientClass { //a CDI component, for example
@EJB
private LocalEjb localEjb;
public void clientMethod() {
localEjb.localEjbMethod();
}
}