users@glassfish.java.net

SOLUTION TO:ClassCastException redeploying WebServices with JPA

From: <glassfish_at_javadesktop.org>
Date: Fri, 02 May 2008 13:08:43 PDT

Since a little while, we got stuck with a real nasty bug in glassfish using javax.persistance api. When redeploying (undeploy and deploy) a beb service application, we had a java.lang.ClassCastException that did not occured if we stopped glassfish before redeploying (or restarting the server after redeploying).

This class cast had nothing to do with the code itself. Doing it with a simple web example (http://www.horstmann.com/elvis/hated-error-messages.html and http://www.iasandcb.pe.kr/blog/2006/01/first-jpa.html). It seems to come from how glassfish handled undeploying the container.

SOLUTION:
close the entity manager factory after usage. It is not pretty and loads the garbage collection, but it work!!!!
public List getContextList(){
   EntityManager em = emf.getEntityManager();
   try {
       javax.persistence.Query q = em.createQuery("select r from Context as r");
       return q.getResultList();
   } catch (NoResultException noResultException) {
       return null;
   } finally {
       cleanup();
   }
}
    public synchronized void cleanup() {
        if (emf != null && emf.isOpen()) {
            emf.close();
        }
        emf = null;
    }

    private synchronized EntityManager getEntityManager() {
        if (emf == null) {
            emf = Persistence.createEntityManagerFactory("server-logic-PU");
        }
        return emf.createEntityManager();
    }
keyword: toplink, classcastexception, glassfish, undeploy, deploy, webservices, JPA
[Message sent by forum member 'boubet' (boubet)]

http://forums.java.net/jive/thread.jspa?messageID=272548