Question

I am trying to understand under what conditions destroySubContext fails in Tomcat. I’m trying to destroy Context which I had created, because of which I’m unable to createContext the next time I start the service. My code is below :

  private Context srvCtx;
  public void init(ServletConfig servletconfig) throws ServletException {
      super.init(servletconfig);
      Context initCtx = new InitialContext();
      srvCtx = initCtx.createSubcontext("myapp");
  }

  public void destroy() 
  {
      try
      {
          if (srvCtx != null)
          {
              srvCtx.destroySubcontext("myapp");
              srvCtx.close();                                                   
          }
      }
      catch(NamingException e)
      {
          log_.error(new LogQueueMessageStructure("Couldn't unbind the context",3016),e);
      }

  } 

What is wrong with my code and how can I debug this issue?

Was it helpful?

Solution

You are creating the context as a subcontext of initCtx:

srvCtx = initCtx.createSubcontext("myapp");

When you are destroing it however it seems like you are trying to destroy it as a subcontext of srvCtx.

srvCtx.destroySubcontext("myapp");

The myapp context is not a subcontext of srvCtx, it is a subcontext of initCtx. It should work better with

initCtx.destroySubcontext("myapp");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top