ClassCastException in the EclipseLink-based web app

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


Comments:

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

Posted by guest on September 27, 2012 at 09:13 AM CEST #

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?

Posted by guest on July 28, 2013 at 11:41 PM CEST #

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?

Posted by Manoj on January 07, 2014 at 11:42 AM CET #

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>

Posted by mufiz on March 06, 2014 at 09:32 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Oracle ECEMEA Partner Hubs Migration Center Team

We share our skills to maximize your revenue!
Our dedicated team of consultants can rapidly and successfully assist you to adopt and implement the latest of Oracle Technology in your solutions.

Stay Connected
partner.imc
@
beehiveonline.oracle-DOT-com
Google+

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
2
3
4
5
6
9
10
11
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today