Different classloaders cause ClassCastException when persisting data via Spring

回眸只為那壹抹淺笑 提交于 2019-12-12 12:08:28

问题


I'm creating an MVC Spring webapp. Using: Jetty (servlet container), DataNucleus (dao platform), DB4O (embedded datastore).

When I persist an object (done from within a Spring Controller) using JDO from DataNucleus, it stores to the DB fine.

@PersistenceCapable
public class Test {
    @Persistent
    private String testString;
    //getter-setters implemented
}

When I do a simple query for the objects I previously added I get a ClassCastException on my Test class (can't cast a.b.c.Test to a.b.c.Test).

The classloader of Test returned by JDO is (toString) [sun.misc.Launcher$AppClassLoader@5acac268], the classloader of the Test class before I persisted it to the DB is [WebAppClassLoader@1593275665]

I've gotten this far, but I don't really know what to do with a classloader issue like this, I've never spent much thought on classloaders before. Any direction is helpful.


回答1:


There doesn't need to be two different versions of the class for a class cast exception to appear. Even the same class definition is seen as two different classes when loaded by two distinct classloaders. Which seems to be the case here.

Unfortunately I am not familiar with the platforms you use, so I can't give more concrete advice than this: try to experiment with moving the jar containing your Test class to different places on your web app classpath, and/or reconfiguring the Spring and Jetty classloaders so that both delegate the loading of Test to the same parent classloader.




回答2:


I think that your problem might be similar to the one described here.

If so, the cure would appear to be to make sure that the jdo jarfile is loaded by a common ancestor of the classloaders.



来源:https://stackoverflow.com/questions/4013047/different-classloaders-cause-classcastexception-when-persisting-data-via-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!