5 Replies Latest reply: Jul 1, 2011 8:29 AM by EJP RSS

    How to reset InitialContext

    user614224
      I am somewhat of a J2EE newbie, so this may be a simple task. I have a J2EE servlet that uses an EJB. The EBJ (indirectly) then has to
      interact with an external JMS provider. The servlet created the EJB as such ("RequestDispatcherBean" is the name of the bean class):

      Context ctx = new InitialContext();
      RequestDispatcherHome requestDispatcherHome = (RequestDispatcherHome)ctx.lookup("java:comp/env/ejb/RequestDispatcherBean");

      The EJB code has to load the JMS provider library that is going to be called, so we have code like this:

                environment.put("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory");
                environment.put("java.naming.provider.url","t3://demoserver:9001");
                InitialContext iniCtx = new InitialContext(environment);

      ... remaining JMS connection/queue setup and execution...

      The first time the servlet creates the EJBm which in turn, calls the JMS code, everything works. However, the second time the sevlet is
      invoked, I get this error: javax.naming.NameNotFoundException: remaining name: env/ejb/RequestDispatcherBean.

      This makes sense now, as I am changing the context, and then when the servlet is invoked again, the "original" context is gone.
      So, the question I have, is there a way to cache the original context's environment, then restore it in the servlet once the JMS session is completed, such that the next
      time the servlet is invoked, the environment is correct?