title: Closing remote EJB connection during managed transaction
body: <p>I have a following situation:</p>
  
  <ol>
  <li>Client is calling Stateless local EJB - managed transaction begins with this invocation</li>
  <li>Local EJB builds InitialContext and looks up remote EJB</li>
  <li>Local EJB invokes method on remote EJB</li>
  <li>Local EJB closes context and connection to remote EJB</li>
  <li>Container tries to commit transaction</li>
  </ol>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Following pseudo code illustrates my issue:</p>
  
  <pre><code>@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();
      }
  }
  </code></pre>