问题
I'm setting up a GWT application based on GWT/RequestFactory on client side, and JPA/EclipseLink over WebLogic on server side.
However, during development, everytime I change something on the server-side code (it can be an entity, the DAO, or even a comment in a Java file!), I get a ClassCastException
between 2 apparently similar classes when I try to use my application, and the only way to get rid of it is to restart my WebLogic server. Even redeploying the application doesn't help.
Feb 10, 2012 4:08:10 PM com.google.web.bindery.requestfactory.server.RequestFactoryServlet doPost SEVERE: Unexpected error java.lang.ClassCastException: com.mycompany.MyClass cannot be cast to com.mycompany.MyClass at com.mycompany.server.locator.CodeLevelLocator.getId(MyClassLocator.java:1) at com.google.web.bindery.requestfactory.server.LocatorServiceLayer.doGetId(LocatorServiceLayer.java:168) at com.google.web.bindery.requestfactory.server.LocatorServiceLayer.getId(LocatorServiceLayer.java:66) at com.google.web.bindery.requestfactory.server.ServiceLayerDecorator.getId(ServiceLayerDecorator.java:81)
Any idea how to avoid this server restart?
Thanks.
EDIT: I'm setting up a bounty on this, because restarting WebLogic after each server change is really painful!!!
EDIT2 solved thanks to James by adding the following ServletContextListener:
public class DeploymentListener implements ServletContextListener {
private static Logger log = LoggerFactory
.getLogger(DeploymentListener.class.getName());
@Override
public void contextDestroyed(ServletContextEvent arg0) {
try {
// close the Entity Manager Factory.
EMF.close();
} catch (Exception e) {
log.error("Error closing the Entity Manager Factory", e);
}
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
// nothing to do
}
}
回答1:
This is a class loader issue, somehow after redeploying you have the old classes/instances remaining somewhere in your application.
It could be related to JPA, are you using a container managed persistence unit, or application managed? Container managed should not have this issue, but application managed could, as if you don't close the old EntityManagerFactory it can remain with the old classes. Ensure you close all factories before redeploying, or try container managed to see if it resolves the issue.
回答2:
Not familiar with running GWT on Weblogic but perhaps this will help.
Run your server in debug mode - it should be able to pick up the class/jar changes at runtime.
Perhaps you only need to re-publish your server instead of restarting it.
Lastly, do you have that class in a jar file elsewhere in your class path?
来源:https://stackoverflow.com/questions/9230092/classcastexception-with-gwt-requestfactory-on-weblogic