glassfish
  1. glassfish
  2. GLASSFISH-19607

ClassCastException when load JPA Entity at start up time

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 3.1.2
    • Fix Version/s: None
    • Component/s: classloader
    • Labels:
      None
    • Environment:

      GlassFish 3.1.2, EclipseLink 2.4.1

      Description

      I have a war file using JPA(EclipseLink). Inside the war file, there is one Message-driven bean and no other EJBs.
      If I reload the web application repeatedly by touch .reload file, I will get ClassCastException.

       java.lang.ClassCastException: com.flexdms.flexims.db.BlobData cannot be cast to com.flexdms.flexims.db.BlobData
      	at com.flexdms.flexims.Types.reLoadTypes(Types.java:190)
      	at com.flexdms.flexims.607341762$Proxy$_$$_WeldSubclass.reLoadTypes(607341762$Proxy$_$$_WeldSubclass.java)
      	at com.flexdms.flexims.Types.init(Types.java:157)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      

      This piece of code is invoked as ServletContextListener to initialize the application. The Class is a JPA Entity Class.

      Others also noticed this issue:http://mcqueeney.com/blog/two-reasons-to-prefer-hibernate-jpa-over-eclipselink-on-glassfish/

      A complete server restart always solves the issue. I suspect the the JPA Entity Class somehow gets into server class loader such as EclipseLink class. A reload creates a new copy of the class and the new copy will conflict with the old copy in server.

      But running two copies of the same application under the same instance has no problem.
      Whether use "delegate" class loading or not has no effect on problem.

      I will see whether I can create a simple application to reproduce this or not.

        Activity

        Hide
        jasonzhang2002gmailcom added a comment - - edited

        I added class loader debug. Here is more information.

        Initial class loading for problematic class. Please note that Entity Class is loaded twice.

        [Loaded com.flexdms.flexims.db.BlobData from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobData.class]
        [Loaded com.flexdms.flexims.db.BlobData from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobData.class]
        [Loaded com.flexdms.flexims.db.BlobDataPK from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobDataPK.class]
        

        Class loading after touching .reload file. BlobData is loaded twice again. But the order is different from previous loading. I will have ClassCastException this time.

        [Loaded com.flexdms.flexims.db.BlobData from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobData.class]
        [Loaded com.flexdms.flexims.db.BlobDataPK from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobDataPK.class]
        [Loaded com.flexdms.flexims.db.BlobData from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobData.class]
        

        By Searching the class loading log file, I notice One loading is clustered together with Entity Class. So it should be from JPA. The other should be from CDI(weld) or other mechanism.

        Moving the initialization code from ServletContextListener to Servlet(Using Load On Startup) does not help.

        Show
        jasonzhang2002gmailcom added a comment - - edited I added class loader debug. Here is more information. Initial class loading for problematic class. Please note that Entity Class is loaded twice. [Loaded com.flexdms.flexims.db.BlobData from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobData.class] [Loaded com.flexdms.flexims.db.BlobData from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobData.class] [Loaded com.flexdms.flexims.db.BlobDataPK from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobDataPK.class] Class loading after touching .reload file. BlobData is loaded twice again. But the order is different from previous loading. I will have ClassCastException this time. [Loaded com.flexdms.flexims.db.BlobData from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobData.class] [Loaded com.flexdms.flexims.db.BlobDataPK from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobDataPK.class] [Loaded com.flexdms.flexims.db.BlobData from file:/opt/glassfish3/glassfish/domains/domain1/applications/flexims_20130131/WEB-INF/classes/com/flexdms/flexims/db/BlobData.class] By Searching the class loading log file, I notice One loading is clustered together with Entity Class. So it should be from JPA. The other should be from CDI(weld) or other mechanism. Moving the initialization code from ServletContextListener to Servlet(Using Load On Startup) does not help.
        Hide
        jasonzhang2002gmailcom added a comment -

        I add some static initialization code to problematic class to track class loading.

        static {
        		System.out.println("load BlobData");
        	}
        

        The class seems only loaded once by JPADeployer although there are two lines in class loading log.
        "reload" seems to be a shortcut to a full unload and load again.

        Here is initial loading sequence

        CONFIG: The target entity (reference) class for the many to one mapping element [method getTypeACL] is being defaulted to: class com.flexdms.flexims.accesscontrol.TypeACL.
        CONFIG: The target entity (reference) class for the one to many mapping element [method getPermissions] is being defaulted to: class com.flexdms.flexims.accesscontrol.Permission.
        CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.PropertyPermission] is being defaulted to: PropertyPermission.
        CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.Permission] is being defaulted to: Permission.
        CONFIG: The column name for element [getId] is being defaulted to: ID.
        CONFIG: The column name for element [getDecision] is being defaulted to: DECISION.
        CONFIG: The column name for element [getPropid] is being defaulted to: PROPID.
        CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.RolePermission] is being defaulted to: RolePermission.
        CONFIG: The column name for element [getRoleid] is being defaulted to: ROLEID.
        CONFIG: The alias name for the entity class [class com.flexdms.flexims.db.BlobData] is being defaulted to: BlobData.
        CONFIG: The column name for element [name] is being defaulted to: NAME.
        CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.TypeACL] is being defaulted to: TypeACL.
        CONFIG: The column name for element [getPTypes] is being defaulted to: PTYPES.
        CONFIG: The column name for element [getTypeid] is being defaulted to: TYPEID.
        CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.InstanceACE] is being defaulted to: InstanceACE.
        CONFIG: The column name for element [id] is being defaulted to: ID.
        CONFIG: The column name for element [typeid] is being defaulted to: TYPEID.
        CONFIG: The column name for element [getRoleid] is being defaulted to: ROLEID.
        CONFIG: The column name for element [getDecision] is being defaulted to: DECISION.
        CONFIG: The column name for element [instanceid] is being defaulted to: INSTANCEID.
        CONFIG: The primary key column name for the mapping element [method getTypeACL] is being defaulted to: TYPEID.
        
        CONFIG: Class com.flexdms.flexims.accesscontrol.TypeACL could not be weaved for change tracking as it is not supported by its mappings.
        INFO: load BlobData
        INFO: EclipseLink, version: Eclipse Persistence Services - 2.4.1.v20121003-ad44345
        CONFIG: connecting(DatabaseLogin(
        	platform=>DerbyPlatform
        	user name=> ""
        	connector=>JNDIConnector datasource name=>null
        ))
        
        CONFIG: Connected: jdbc:derby:/opt/glassfish3/javadb/flexims/flexims
        	User: flexims
        	Database: Apache Derby  Version: 10.8.1.2 - (1095077)
        	Driver: Apache Derby Embedded JDBC Driver  Version: 10.8.1.2 - (1095077)
        CONFIG: connecting(DatabaseLogin(
        	platform=>DerbyPlatform
        	user name=> ""
        	connector=>JNDIConnector datasource name=>null
        ))
        CONFIG: Connected: jdbc:derby:/opt/glassfish3/javadb/flexims/flexims
        	User: flexims
        	Database: Apache Derby  Version: 10.8.1.2 - (1095077)
        	Driver: Apache Derby Embedded JDBC Driver  Version: 10.8.1.2 - (1095077)
        
        INFO: file:/opt/glassfish3/glassfish/domains/domain1/eclipseApps/flexims/WEB-INF/classes/_flexims login successful
        WARNING: Multiple [2] JMX MBeanServer instances exist, we will use the server at index [0] : [com.sun.enterprise.v3.admin.DynamicInterceptor@148ba3d].
        WARNING: JMX MBeanServer in use: [com.sun.enterprise.v3.admin.DynamicInterceptor@148ba3d] from index [0] 
        WARNING: JMX MBeanServer in use: [com.sun.jmx.mbeanserver.JmxMBeanServer@f77965] from index [1] 
        
        INFO: JTS5014: Recoverable JTS instance, serverId = [3700]
        
        INFO: JMS013: end point determine destionation name, Res name: javax.jms.Queue, JNDI name: BackgroundRunner descriptor name : {3}
        
        INFO: WELD-000900 1.1.4 (Final)
        
        WARNING: DPL8007: Unsupported deployment descriptors element message-destination value null
        
        INFO: Registering the Jersey servlet application, named com.flexdms.flexims.json.RsActivator, at the servlet mapping, /json/*, with the Application class of the same name
        INFO: Initializing Mojarra 2.2.0-m06 (-SNAPSHOT 20121109-2001 https://svn.java.net/svn/mojarra~svn/tags/2.2.0-m06@10994) for context '/flexims'
        
        INFO: Monitoring jndi:/server/flexims/WEB-INF/faces-config.xml for modifications
        INFO: Running on PrimeFaces 3.4.2
        INFO: acl is constructed
        INFO: PWC1412: WebModule[null] ServletContext.log():com.flexdms.flexims.InitServlet: initialization is called in InitServlet
        

        The loading sequence is JPA weaving (EclipseLink), DB Resource creation, JSF(mojarra, maybe from FaceServlet), my initialization code.

        Here is the loading sequence from reloading

        
        INFO: Registering the Jersey servlet application, named com.flexdms.flexims.json.RsActivator, at the servlet mapping, /json/*, with the Application class of the same name
        INFO: Initializing Mojarra 2.2.0-m06 (-SNAPSHOT 20121109-2001 https://svn.java.net/svn/mojarra~svn/tags/2.2.0-m06@10994) for context '/flexims'
        
        INFO: Monitoring jndi:/server/flexims/WEB-INF/faces-config.xml for modifications
        INFO: Running on PrimeFaces 3.4.2
        INFO: acl is constructed
        INFO: PWC1412: WebModule[null] ServletContext.log():com.flexdms.flexims.InitServlet: initialization is called in InitServlet
        INFO: fileUploadDir =/opt/glassfish3/glassfish/domains/domain1/fleximsFiles/flexims
        FINE: SELECT NAME, datatype, data FROM BLOBDATA WHERE (datatype = ?)
        	bind => [type]
        SEVERE: WebModule[/flexims]StandardWrapper.Throwable
        java.lang.RuntimeException: java.lang.ClassCastException: com.flexdms.flexims.db.BlobData cannot be cast to com.flexdms.flexims.db.BlobData
        

        The JPA and DB Resource creation are skipped. Does it mean JPA will keep old class reference?

        Show
        jasonzhang2002gmailcom added a comment - I add some static initialization code to problematic class to track class loading. static { System .out.println( "load BlobData" ); } The class seems only loaded once by JPADeployer although there are two lines in class loading log. "reload" seems to be a shortcut to a full unload and load again. Here is initial loading sequence CONFIG: The target entity (reference) class for the many to one mapping element [method getTypeACL] is being defaulted to: class com.flexdms.flexims.accesscontrol.TypeACL. CONFIG: The target entity (reference) class for the one to many mapping element [method getPermissions] is being defaulted to: class com.flexdms.flexims.accesscontrol.Permission. CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.PropertyPermission] is being defaulted to: PropertyPermission. CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.Permission] is being defaulted to: Permission. CONFIG: The column name for element [getId] is being defaulted to: ID. CONFIG: The column name for element [getDecision] is being defaulted to: DECISION. CONFIG: The column name for element [getPropid] is being defaulted to: PROPID. CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.RolePermission] is being defaulted to: RolePermission. CONFIG: The column name for element [getRoleid] is being defaulted to: ROLEID. CONFIG: The alias name for the entity class [class com.flexdms.flexims.db.BlobData] is being defaulted to: BlobData. CONFIG: The column name for element [name] is being defaulted to: NAME. CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.TypeACL] is being defaulted to: TypeACL. CONFIG: The column name for element [getPTypes] is being defaulted to: PTYPES. CONFIG: The column name for element [getTypeid] is being defaulted to: TYPEID. CONFIG: The alias name for the entity class [class com.flexdms.flexims.accesscontrol.InstanceACE] is being defaulted to: InstanceACE. CONFIG: The column name for element [id] is being defaulted to: ID. CONFIG: The column name for element [typeid] is being defaulted to: TYPEID. CONFIG: The column name for element [getRoleid] is being defaulted to: ROLEID. CONFIG: The column name for element [getDecision] is being defaulted to: DECISION. CONFIG: The column name for element [instanceid] is being defaulted to: INSTANCEID. CONFIG: The primary key column name for the mapping element [method getTypeACL] is being defaulted to: TYPEID. CONFIG: Class com.flexdms.flexims.accesscontrol.TypeACL could not be weaved for change tracking as it is not supported by its mappings. INFO: load BlobData INFO: EclipseLink, version: Eclipse Persistence Services - 2.4.1.v20121003-ad44345 CONFIG: connecting(DatabaseLogin( platform=>DerbyPlatform user name=> "" connector=>JNDIConnector datasource name=> null )) CONFIG: Connected: jdbc:derby:/opt/glassfish3/javadb/flexims/flexims User: flexims Database: Apache Derby Version: 10.8.1.2 - (1095077) Driver: Apache Derby Embedded JDBC Driver Version: 10.8.1.2 - (1095077) CONFIG: connecting(DatabaseLogin( platform=>DerbyPlatform user name=> "" connector=>JNDIConnector datasource name=> null )) CONFIG: Connected: jdbc:derby:/opt/glassfish3/javadb/flexims/flexims User: flexims Database: Apache Derby Version: 10.8.1.2 - (1095077) Driver: Apache Derby Embedded JDBC Driver Version: 10.8.1.2 - (1095077) INFO: file:/opt/glassfish3/glassfish/domains/domain1/eclipseApps/flexims/WEB-INF/classes/_flexims login successful WARNING: Multiple [2] JMX MBeanServer instances exist, we will use the server at index [0] : [com.sun.enterprise.v3.admin.DynamicInterceptor@148ba3d]. WARNING: JMX MBeanServer in use: [com.sun.enterprise.v3.admin.DynamicInterceptor@148ba3d] from index [0] WARNING: JMX MBeanServer in use: [com.sun.jmx.mbeanserver.JmxMBeanServer@f77965] from index [1] INFO: JTS5014: Recoverable JTS instance, serverId = [3700] INFO: JMS013: end point determine destionation name, Res name: javax.jms.Queue, JNDI name: BackgroundRunner descriptor name : {3} INFO: WELD-000900 1.1.4 (Final) WARNING: DPL8007: Unsupported deployment descriptors element message-destination value null INFO: Registering the Jersey servlet application, named com.flexdms.flexims.json.RsActivator, at the servlet mapping, /json/*, with the Application class of the same name INFO: Initializing Mojarra 2.2.0-m06 (-SNAPSHOT 20121109-2001 https: //svn.java.net/svn/mojarra~svn/tags/2.2.0-m06@10994) for context '/flexims' INFO: Monitoring jndi:/server/flexims/WEB-INF/faces-config.xml for modifications INFO: Running on PrimeFaces 3.4.2 INFO: acl is constructed INFO: PWC1412: WebModule[ null ] ServletContext.log():com.flexdms.flexims.InitServlet: initialization is called in InitServlet The loading sequence is JPA weaving (EclipseLink), DB Resource creation, JSF(mojarra, maybe from FaceServlet), my initialization code. Here is the loading sequence from reloading INFO: Registering the Jersey servlet application, named com.flexdms.flexims.json.RsActivator, at the servlet mapping, /json/*, with the Application class of the same name INFO: Initializing Mojarra 2.2.0-m06 (-SNAPSHOT 20121109-2001 https: //svn.java.net/svn/mojarra~svn/tags/2.2.0-m06@10994) for context '/flexims' INFO: Monitoring jndi:/server/flexims/WEB-INF/faces-config.xml for modifications INFO: Running on PrimeFaces 3.4.2 INFO: acl is constructed INFO: PWC1412: WebModule[ null ] ServletContext.log():com.flexdms.flexims.InitServlet: initialization is called in InitServlet INFO: fileUploadDir =/opt/glassfish3/glassfish/domains/domain1/fleximsFiles/flexims FINE: SELECT NAME, datatype, data FROM BLOBDATA WHERE (datatype = ?) bind => [type] SEVERE: WebModule[/flexims]StandardWrapper.Throwable java.lang.RuntimeException: java.lang.ClassCastException: com.flexdms.flexims.db.BlobData cannot be cast to com.flexdms.flexims.db.BlobData The JPA and DB Resource creation are skipped. Does it mean JPA will keep old class reference?
        Hide
        jasonzhang2002gmailcom added a comment -

        Here is a workaround.
        Copy all org.eclipse.persistence jar from glassfish3/glassfish/modules to WEB-INF/lib.
        change glassfish-web, set class delegate=false.

        The web application can be reloaded repeatedly. The reload sequence will be the same as initial loading sequence.

        Show
        jasonzhang2002gmailcom added a comment - Here is a workaround. Copy all org.eclipse.persistence jar from glassfish3/glassfish/modules to WEB-INF/lib. change glassfish-web, set class delegate=false. The web application can be reloaded repeatedly. The reload sequence will be the same as initial loading sequence.
        Hide
        jasonzhang2002gmailcom added a comment -

        If I use EclipseLink inside of my war, I eventually have

        Caused by: java.lang.OutOfMemoryError: PermGen space
        	at java.lang.ClassLoader.defineClass1(Native Method)
        	at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
        	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
        

        after several iteration

        Show
        jasonzhang2002gmailcom added a comment - If I use EclipseLink inside of my war, I eventually have Caused by: java.lang.OutOfMemoryError: PermGen space at java.lang. ClassLoader .defineClass1(Native Method) at java.lang. ClassLoader .defineClass( ClassLoader .java:791) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) after several iteration
        Hide
        ceyhun added a comment -

        I have same exception. Maybe works for you, I clean and build project and , stop-start glassfish server and deploy again.

        Show
        ceyhun added a comment - I have same exception. Maybe works for you, I clean and build project and , stop-start glassfish server and deploy again.

          People

          • Assignee:
            Sanjeeb Sahoo
            Reporter:
            jasonzhang2002gmailcom
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated: