Using ServletContextListener and JNDI to run a clean Web Application shutdown

Key Facts and Overview

  • ServletContextListener runs contextInitialized() during a deployment operation
  • ServletContextListener runs contextDestroyed() during a un-deployment operation
  • This allows to cleanup or initialize certain objects factories, loggers, ..
  • Without a clean shutdown you may encounter following Exception during redeployment of a WEB applications
     12:54:21.020 Object: com.hhu.wfjpa2el.Emp2[ empno=9997 ] is not a known entity type.
     12:54:21.020 java.lang.IllegalArgumentException: Object: com.hhu.wfjpa2el.Emp2[ empno=9997 ] is not a known entity type.
         at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(
         at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(
         at com.hhu.wfjpa2el.JPATestBean.runJPA(
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  • To solve this reboot your Webserver or shutdown() the Entity Manager Factory


  • JPATestBean() constructor stores/binds a object reference of the current  JPATestBean() reference via JNDI
  • shutdown() is invoked by  ServletContextListener function contextDestroyed() and stops the Entity Manager Factory.
  • ServletContextListener is using JNDI lookup to get the object reference  
public JPATestBean() throws Exception
        ctx = new InitialContext();
        ctx.rebind(jndiName, this); "Constructor JPATestBean()  called and bound to JNDI !");
        emf = Persistence.createEntityManagerFactory("jpaELPU");
        threadLocal = new ThreadLocal<EntityManager>();       
 public void shutdown()
      {"\nWeb Application shutdown ..." );
        closeEntityManager();"Closing Entitiy Manager Factory ..." );
        System.gc();"Leaving Web Application shutdown" );

Implementing ServletContextListener 
  -  contextDestroyed() uses JNDI lookup to read an object reference from our  JPATestBean() instance
  -  contextDestroyed() invokes shutdown methode to run a clean application shutdown

web.xml entry

Java Code for ServletContextListener
package com.hhu.wfjpa2el;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.slf4j.LoggerFactory;

public class ServletContextListenerImpl implements ServletContextListener 
    final static org.slf4j.Logger logger = LoggerFactory.getLogger(ServletContextListenerImpl.class);
    private JPATestBean jtb;    
    private final String jndiName = "java:global/myJPATestBean";
    private InitialContext ctx;
    public void contextInitialized(ServletContextEvent arg) {"+++ ServletContextListener : contextInitialized - no action \n");

    public void contextDestroyed(ServletContextEvent arg) 
        System.out.println("+++ ServletContextListener: running contextDestroyed()....");
            ctx = new InitialContext();
            jtb = (JPATestBean)ctx.lookup(jndiName);
  "+++ ServletContextListener:  Leaving contextDestroyed() without Exception\n");
          } catch (NamingException ex)
            //Logger.getLogger(ServletContextListenerImpl.class.getName()).log(Level.SEVERE, null, ex);
            logger.error("+++ Error in contextDestroyed()\n" + ex);



Leave a Reply

Your email address will not be published. Required fields are marked *