0 Replies Latest reply on Jan 11, 2012 7:25 PM by ekizhner

    ClassCastException because of different class loaders (UnifiedClassLoader3 and WebappClassLoader)

    ekizhner

      I am trying to find a way to get around the class loader issues I started having with an existing application (.ear).

       

      This happens with JBoss version 4.2.3. Hibernate version 3.2.4.

      Tried to separate our application ear through <loader-repository> tag in jboss-app.xml. This did not alter the outcome, i.e. still ClassCastException.

      We have another application ear that loads the same object/objects, and when I altered the second .ear and put <loader-repository> tag in its jboss-app.xml, the first ear (our application .ear) failed to deploy because it could not find some class (NoClassDefFound)

       

      It would probably be a lot of work to try to separate the ears, because the hibernate objects (the ones that are having cast issues right now) are the same. There is a jar that houses these objects and both .ear files refer to it. It is very probably that the other app is loaded first with a UnifiedClassLoader3.

       

      Also, and this may be relevant, the problem occurs when running jobs under Quartz -- stack trace below:

       

      java.lang.ClassCastException: com.tools.common.database.model.vmc.TaskStatus cannot be cast to com.tools.common.database.model.vmc.TaskStatus

          at com.tools.common.services.impl.ScheduledTaskServiceImpl$GetStatusByName.runCall(ScheduledTaskServiceImpl.java:154)

          at com.tools.common.services.impl.ScheduledTaskServiceImpl$GetStatusByName.runCall(ScheduledTaskServiceImpl.java:145)

          at com.tools.common.services.HibernateExecutor.execute(HibernateExecutor.java:45)

          at com.vmc.jobs.ScheduledTaskJob.initializeResult(ScheduledTaskJob.java:28)

          at com.vmc.jobs.OrphanDataTask.execute(OrphanDataTask.java:43)

          at org.quartz.core.JobRunShell.run(JobRunShell.java:203)

          at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)

       

      Put in a try/catch around the ClassCastException and printed out the following info about classloaders:

      [DEBUG] 11 Jan 2012 14:30:00,576 T[DefaultQuartzScheduler_Worker-8] DICTATION Class Loader Info: class[class com.tools.common.database.model.vmc.TaskType] class.hashCode[2021471421] cl.toString[org.jboss.mx.loading.UnifiedClassLoader3@25aa370b{ url=file:/C:/Program Files (x86)/nVoq/Vcore/ThirdPartyApps/JBoss/server/default/tmp/deploy/tmp1480424414644123201dictation.ear ,addedOrder=69}] cl.hashCode[631912203] cl.class[class org.jboss.mx.loading.UnifiedClassLoader3]
      [DEBUG] 11 Jan 2012 14:30:00,577 T[DefaultQuartzScheduler_Worker-8] DICTATION Class Loader Info: class[class com.tools.common.database.model.vmc.TaskType] class.hashCode[406941515] cl.toString[WebappClassLoader

       

      Note that I made a change to OrphanDataTask (from the stack trace above) to initialize a Hibernate SessionFactory. This seems to be something that precipitated this issue with the class loaders and ClassCastException.

       

      Line 154 that caused a problem looks like this:

      TaskStatus status = (TaskStatus)session.createQuery("FROM TaskStatus WHERE name = :name")

                                                         .setString("name", name)

                                                         .uniqueResult();

      Thanks in advance for any suggestions!