Topic
6 replies Latest Post - ‏2013-02-22T18:49:46Z by SystemAdmin
SystemAdmin
SystemAdmin
37422 Posts
ACCEPTED ANSWER

Pinned topic JNDI giving EntityManager sometimes and EntityManagerFactory other times

‏2013-02-06T23:21:14Z |
I can't seem to figure out why the same code on two separate WAS 7 installations is giving me two different objects. One environment is local and the other is on a VPS. I installed both environments myself. I had this problem a while ago, but only after I updated my local WAS with patches and not the remote host. When I applied patches to the remote host everything worked. But now, things are back to not working and I haven't changed anything (other than stopping/starting the server and removing some env-entry tags from the web.xml).

ibm-web-bnd.xml
<resource-ref name="jdbc/sg_datasource" binding-name="jdbc/sg_datasource" />

web.xml
<persistence-context-ref>
<persistence-context-ref-name>sg/jpa</persistence-context-ref-name>
<persistence-unit-name>sgweb</persistence-unit-name>
</persistence-context-ref>

<resource-ref id="DataSource_NonTransactional_ResourceRef">
<res-ref-name>jdbc/sg_datasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

META-INF/persistence.xml
<persistence-unit name="sgweb" transaction-type="JTA">
<provider>com.ibm.websphere.persistence.PersistenceProviderImpl</provider>
<jta-data-source>java:comp/env/jdbc/sg_datasource</jta-data-source>
<properties>
<property name="openjpa.jdbc.SchemaFactory" value="native(ForeignKeys=true)" />
<property name="openjpa.jdbc.Schema" value="sg_test"/>
</properties>
</persistence-unit>

Locally:
EntityManager em = (EntityManager) (new InitialContext()).lookup("java:comp/env/sg/jpa");

Error 500: java.lang.ClassCastException: com.ibm.ws.persistence.EntityManagerFactoryImpl incompatible with javax.persistence.EntityManager

Remote:
EntityManagerFactory emf = (EntityManagerFactory) (new InitialContext()).lookup("java:comp/env/sg/jpa");

Error 500: java.lang.ClassCastException: com.ibm.ws.jpa.management.JPATxEntityManager incompatible with javax.persistence.EntityManagerFactory
Why does one environment give me a Manager and the other a Factory?
Updated on 2013-02-22T18:49:46Z at 2013-02-22T18:49:46Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    37422 Posts
    ACCEPTED ANSWER

    Re: JNDI giving EntityManager sometimes and EntityManagerFactory other times

    ‏2013-02-07T13:36:59Z  in response to SystemAdmin
    It seems as if deleting and re-adding the Data Source in the control panel fixes the issue. When I upgraded before I made new profiles, so I had to make all new data source definitions and JDBC drivers.
    • SystemAdmin
      SystemAdmin
      37422 Posts
      ACCEPTED ANSWER

      Re: JNDI giving EntityManager sometimes and EntityManagerFactory other times

      ‏2013-02-11T22:04:17Z  in response to SystemAdmin
      I got it again and deleting the data source in the control panel did not help. I had to shut down the server and delete temp/* and wstemp/* from the profile directory.
      • bpaskin
        bpaskin
        3639 Posts
        ACCEPTED ANSWER

        Re: JNDI giving EntityManager sometimes and EntityManagerFactory other times

        ‏2013-02-12T12:33:54Z  in response to SystemAdmin
        Hi, can you please make sure you do not have any extra jars in your application that might be interfering with class loading? Duplication of WAS functionality may be responsible for the problem. You can always turn on verbose classloading to see where the classes are being loaded from.

        Regards,
        Brian
        • SystemAdmin
          SystemAdmin
          37422 Posts
          ACCEPTED ANSWER

          Re: JNDI giving EntityManager sometimes and EntityManagerFactory other times

          ‏2013-02-22T18:09:07Z  in response to bpaskin
          What kind of jars would I be looking for? This isn't a class cast exception between an EntityManager and an EntityManager caused by too many jars. The JNDI is actually returning an EntityManagerFactory. As far as I know, the following code should not work, unless I'm getting my context/unit/factory/manager relations backwards. Definition persistence-context-ref should give back a manager and persistence-unit-ref should give back the factory, no?
          try {
          em = (EntityManager) (new InitialContext()).lookup("java:comp/env/sbp/jpa");
          } catch (java.lang.ClassCastException e) {
          emf = (EntityManagerFactory) (new InitialContext()).lookup("java:comp/env/sbp/jpa");
          em = emf.createEntityManager();
          }
          web.xml:
          <persistence-context-ref>
          <persistence-context-ref-name>sbp/jpa</persistence-context-ref-name>
          <persistence-unit-name>sbpds</persistence-unit-name>
          </persistence-context-ref>
        • SystemAdmin
          SystemAdmin
          37422 Posts
          ACCEPTED ANSWER

          Re: JNDI giving EntityManager sometimes and EntityManagerFactory other times

          ‏2013-02-22T18:38:04Z  in response to bpaskin
          If I grep for EntityManager in the verbose class loading file, this is what I get:

          ds5071$ grep EntityManager logs/server1/native_stderr.log | grep -v XML
          class load: javax.persistence.EntityManagerFactory from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/javax.j2ee.persistence.jar
          class load: org.apache.openjpa.persistence.EntityManagerFactoryValue from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: org.apache.openjpa.persistence.OpenJPAEntityManagerFactory from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: org.apache.openjpa.persistence.EntityManagerFactoryImpl from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: com.ibm.websphere.persistence.WsJpaEntityManagerFactory from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: com.ibm.ws.persistence.WsJpaEntityManagerFactory from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: com.ibm.ws.persistence.EntityManagerFactoryImpl from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: javax.persistence.EntityManager from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/javax.j2ee.persistence.jar

          ... and after a JPA query

          class load: com.ibm.ws.jpa.management.JPAEntityManager from: file:/home/was/Websphere/SDP/runtimes/base_v7/profiles/AppSrv01/configuration/org.eclipse.osgi/bundles/292/1/.cp/runtime.jpa.jar
          class load: com.ibm.ws.jpa.management.JPATxEntityManager from: file:/home/was/Websphere/SDP/runtimes/base_v7/profiles/AppSrv01/configuration/org.eclipse.osgi/bundles/292/1/.cp/runtime.jpa.jar
          class load: com.ibm.ws.jpa.management.JPAEntityManager$1 from: file:/home/was/Websphere/SDP/runtimes/base_v7/profiles/AppSrv01/configuration/org.eclipse.osgi/bundles/292/1/.cp/runtime.jpa.jar
          class load: com.ibm.ws.jpa.management.JPAEntityManager$2 from: file:/home/was/Websphere/SDP/runtimes/base_v7/profiles/AppSrv01/configuration/org.eclipse.osgi/bundles/292/1/.cp/runtime.jpa.jar
          class load: org.apache.openjpa.persistence.OpenJPAEntityManager from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: org.apache.openjpa.persistence.OpenJPAEntityManagerSPI from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: org.apache.openjpa.persistence.EntityManagerImpl from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: com.ibm.websphere.persistence.WsJpaEntityManager from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: com.ibm.ws.persistence.WsJpaEntityManager from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          class load: com.ibm.ws.persistence.EntityManagerImpl from: file:/home/was/Websphere/SDP/runtimes/base_v7/feature_packs/jpa/plugins/com.ibm.ws.jpa.jar
          • SystemAdmin
            SystemAdmin
            37422 Posts
            ACCEPTED ANSWER

            Re: JNDI giving EntityManager sometimes and EntityManagerFactory other times

            ‏2013-02-22T18:49:46Z  in response to SystemAdmin
            On the server which does not exhibit the class cast exception (currently, they both do from time to time) there is no second part to the class loading output after a query. I'm assuming the class cast exception catch which loads an entitymanagerfactory triggers more class loading. When there is no exception, only the top part of the classes listed above are loaded, when there is an exception and it is caught and a factory is pulled from JNDI then the additional second part of the classes are loaded.