X

@OracleIMC Partner Resources & Training: Discover your Modernization options + Reach new potential through Innovation

ClassCastException in the EclipseLink-based web app

Guest Author
One of the partners asked for a help to find out the root cause of the issue that they had in their web application that uses EclipseLink running on Weblogic 11g.  During the first deployment the application everything works correctly, but after redeployment the following error occures in the place where Servlet invokes POJO method with JPA calls:

java.lang.ClassCastException: model.entity.Department
    at web.DemoServlet.doGet(DemoServlet.java:34)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)


I looked at the code of the POJO and saw the following:

public class DepartmentFacade extends JPAFacade {
    ....   
    public static List<Department> findAllDepartments()
       {
            return getEntityManager().createNamedQuery("Department.findAll").getResultList();
       }   
    ....
}   

And here is a parent class:

public class JPAFacade {
    private static String persistenceUnitName = "EclipseLinkWeb";
    private static EntityManagerFactory emf = Persistence
            .createEntityManagerFactory(persistenceUnitName);


    public static EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    ...
}

As you might see EntityManagerFactory is acquired statically, and there is no code to free the EntityManagerFactory and other resources before the app starts up again.  This causes the underlying resources to still exist, which have links to the old classloader, and this leads to java.lang.ClassCastException after redeployment.

So first of all I've added the following method to JPAFacade class:

public class JPAFacade {
  ...
      public static void releaseResources() {
        if (emf != null) {
            emf.close();
        }       
    }
  ...
}


and after that I've created ServletContextListener to release EntityManagerFactory during application shutdown:

public class DemoListener implements ServletContextListener {

    public DemoListener() {
    }

    public void contextInitialized(ServletContextEvent arg0) {
    }

    public void contextDestroyed(ServletContextEvent arg0) {
        JPAFacade.releaseResources();       
    }   
}

And that's it,  now the JPA resources are released correctly and ClassCastException is gone. I want to thank  Christopher Delahunt who shared his idea on how to solve the issue.

Additional notes related to the EclipseLink support in the latest Weblogic 10.3.4:
  • WLS 10.3.4 is shipped with EclipseLink 2.1.2, located in the $MIDDLEWARE_HOME/modules/org.eclipse.persistence_1.0.0.0_2-1.jar , it uses JPA 1.0 by default
  • to get JPA 2.0 in WebLogic 10.3.4, you will need to follow the documentation provided


Join the discussion

Comments ( 4 )
  • guest Thursday, September 27, 2012

    Thanks, helped me a bunch! I am using glassfish fyi.


  • guest Sunday, July 28, 2013

    This looks great, except I'm not sure how you're accessing JPAFacade within your ServletContextListener. Could you go into more detail about how the DemoListener class is able to instantiate your JPAFacade variable?


  • Manoj Tuesday, January 7, 2014

    Hi there,

    I am facing very similar issue, but I am not using named queries. I have getEntityManager().createQuery() to create the Query object in the code.

    Not setting any hits on the query object, so it uses all the defaults. The application throws classCastException for objects retrieved by query, after webapp is restarted.

    How do we clear the query resultset cache?


  • mufiz Thursday, March 6, 2014

    create a Class JPAFacade:

    ----------------------

    public class JPAFacade implements ServletContextListener{

    @Override

    public void contextDestroyed(ServletContextEvent sce) {

    // TODO Auto-generated method stub

    MyClass.releaseResources();

    }

    @Override

    public void contextInitialized(ServletContextEvent sce) {

    // TODO Auto-generated method stub

    }

    }

    Create a class MyClass

    ----------------------

    public class MyClass {

    public static EntityManager getEntityManager() {

    emf = Persistence

    .createEntityManagerFactory("MyPersistenceUnit");

    em = emf.createEntityManager();

    return em;

    }

    public static void releaseResources() {

    logger.info("resources destroyed");

    if(emf!=null){

    emf.close();

    }

    }

    }

    web.xml

    -------

    <listener>

    <listener-class>com.logic.JPAFacade</listener-class>

    </listener>


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.