views:

586

answers:

3

Hi all,

I get this error:

StandardWrapperValve[Vaadin Servlet]: PWC1406: Servlet.service() for servlet Vaadin Servlet threw exception java.lang.ClassCastException: com.delhi.entities.Category cannot be cast to com.delhi.entities.Category

when I try to run my webapps on glassfish v2.

Category is a JPA entity object

the offending code according to the server log is:

for (Category c : categories) {
          mymethod();
   }

categories is derived from:

List<Category> categories = q.getResultList();

Any idea what went wrong?

+1  A: 

This is a class loader issue. If a class is loaded by different class loaders, it's objects cannot be assigned to each other. You have probably passed an object from one WAR into another one. There are several options to resolve this:

  • Put all your code into a single WAR.
  • Use some form of remoting between your WARs. Serialization takes care of the class loader problem.
  • Try putting all you WARs into a single EAR. If that doesn't work, put all code into JARs that are on the EAR's Classpath in the MANIFEST.MF.
FelixM
A: 

My observation is that it only happens when using a hot redeploy or a static redeploy. This only applies, of course, if you get a class cast exception where both the to and from classes are the same.

Workarounds:

  • Don't use undeploy and deploy instead of redeploy
  • Restart app server
  • Remove static members of the affected classes
  • Use a remote interface (serialization makes this go away)

IMO I think the class loader was unable to reload the class and the old version was reused, resulting in the error.


This article doesn't talk about this error directly, but it is good background info on how the class loader works.

bguiz
A: 

I'm experiencing this problem too with Glassfish v2 and Glassfish v3.

Can I ask you a question: Are you attempting to initialize any persistence object when the application is deployed (through a servlet loaded on startup or a context listener)?

Like bguiz, I've noticed this problem only happens on redeploy. A new deploy to a freshly restarted Glassfish server, never has this problem.

Like FelixM mentioned, I'm convinced this is a class loader issue, however I don't believe it's an issue with multiple wars (I only have 1 deployed to my server). In Glassfish 3, I can see that my WAR is utilizing 2 Glassfish "engines". One for the web(war) and one for the jpa. From what I understand, these are different containers each with their own classloader. I'm guessing Glassfish v2 works in the same manner.

I'm using Spring and (re)initialize some persistence objects on (re)deploy. What I'm thinking, is that while the web engine is reinitializing the war, the jpa engine is still using the old class definitions. Often if I retry the redeploy after this initial failure, it may succeed (sometimes it may take more than one retry but eventually I can get it to succeed without a restart - having better success with Glassfish v3 than v2).

At this point I'm thinking that either these two classloaders are out of sync or there is some sort of race condition on redeploy allowing this operation to sometimes succeed. I've tried to force the classloader, writing code like this

HashMap<Object, Object> properties = new HashMap<Object, Object>();
properties.put(PersistenceUnitProperties.CLASSLOADER, this.getClass().getClassLoader());
entityManagerFactory = Persistence.createEntityManagerFactory(jpaContext, properties);

but it didn't seem to have any affect.

I'm also wondering if eliminating the initialization at startup could fix the problem, giving the appserver time to resynchronize both engines before using any jpa classes (which is why I asked my follow up question).

Vinnie