PojoCache ClassCastException
morisona Apr 26, 2007 10:51 AMIf this is a stupid question (ie the answer is to be found in the docs, examples, this forum, or the wiki) please tell me, and I will go back searching. Otherwise I would be very greatful if someone with experience of PojoCache anf JBoss classloading could give me their view.
I hava a PojoCache deployed on the JBoss As Server (4.0.5). I create and update objects from a Servlet. This works fine until I undeploy and deploy the Web App. I then get a ClassCastException here:
Object obj = cache.getObject("/totos/" + nick); Toto toto = (toast.Toto) obj;
(The object concerned is a 'Toto')
I have "UseJBossWebLoader" in the Tomcat jboss-service.xml set to true. (Otherwise the cache in the 'other' server of the cluster can not find the object).
I have traced the classloaders being used. With the following code:
Object obj = cache.getObject("/totos/" + nick); ... System.out.println("Object retrieved from cache class is: " + obj.getClass().getName() + ", Classloader is:" + obj.getClass().getClassLoader()); System.out.println("Context Classloader is:" + Thread.currentThread().getContextClassLoader()); System.out.println("Class loader of a new class is:" + new Toto().getClass().getClassLoader()); Toto toto = (toast.Toto) obj;
This gives:
Object retrieved from cache class is: toast.Toto, Classloader is:org.jboss.mx.loading.UnifiedClassLoader3@9c67db{ url=null ,addedOrder=57} Context Classloader is:org.jboss.web.tomcat.tc5.WebCtxLoader$ENCLoader@feb215 Class loader of a new class is:org.jboss.mx.loading.UnifiedClassLoader3@bf4472{ url=file:/C:/jboss/jboss-4.0.5.GAejb3/server/default/tmp/deploy/tmp60992simple-exp.war/ ,addedOrder=58}
So the classloader used to load the object from the cache (UCL, addorder=57) is not the same as that used to create an object in this environment (UCL, addorder=58). So I can understand that I get a ClasscastException.
I have tried registering the classloader used by the servlet:
MBeanServer server = MBeanServerLocator.locateJBoss(); cache = (PojoCacheMBean) MBeanProxyExt.create(PojoCacheMBean.class, "jboss.cache:service=PojoCache", server); ClassLoader cl = new Toto().getClass().getClassLoader(); cache.registerClassLoader("/totos", cl);
But this has had no effect.
I have also tried setting the ContextClassLoader to the ClassLoader used by the cache:
ClassLoader cl = obj.getClass().getClassLoader(); Thread.currentThread().setContextClassLoader(cl);
But that had no effect either.
Thank you.