11 Replies Latest reply on Dec 10, 2008 12:10 PM by alrubinger

    EAR Scoped PersistenceUnit

    mzeijen

      Let's say that I create the following package:

      - app.ear
      - /lib
      - pu.jar
      - /META-INF/persistence.xml
      - aEJB3Lib.jar
      - DAOEJB3Lib.jar

      The persistence unit defined in the persistence.xml of /lib/pu.jar should be EAR scoped and not Globally Scoped (using a ear scoped classloader). Am I right? At least I understood this from the documentation. The problem is that in JBoss 4.2.1 it looks like that the persistence unit becomes globally. I will explain what the effects are.

      As you can see I have this DAOEJB3Lib.jar. It contains the entity classes but also some DAO Stateless beans. Those DAO's get there persistence manager using the unit name defined in the persistence.xml of /lib/pu.jar (lets say it is called pu1). I used this construction so that I can reuse those DAO's for different applications. Because the persistence unit name is hard coded in the DAO's of the DAOEJB3Lib.jar lib we need to define the same name in the persistence.xml. This means that if the DAOEJB3Lib.jar is reused by multiple applications on the same server there will also exist multiple persistence units with the same. If the persistence unit is ear scoped then this setup wouldn't create any problems. Practically however this doesn't work.

      All works fine if one application with this DAOEJB3Lib.jar lib is deployed on the server. The problems begin when I deploy a second application that contains the same DAOEJB3Lib.jar. The first application won't work any more because it now retrieves the persistence unit from the second application...

      Did I understand the Documents wrong or is there a Bug in the EJB3.0 lib of JBoss 4.2.1?

        • 1. Re: EAR Scoped PersistenceUnit
          mzeijen

          Hmm, I forgot to put code around the packaging example. Here is it again:

          - app.ear
           - /lib
           - pu.jar
           - /META-INF/persistence.xml
           - aEJB3Lib.jar
           - DAOEJB3Lib.jar
          


          • 2. Re: EAR Scoped PersistenceUnit

            Have you defined your class loader reporitory in jboss-app.xml?

            Regards

            Felix

            • 3. Re: EAR Scoped PersistenceUnit
              mzeijen

              Yes, I have. Looks like this:

              <!DOCTYPE jboss-app
               PUBLIC "-//JBoss//DTD J2EE Application 4.2//EN"
               "http://www.jboss.org/j2ee/dtd/jboss-app_4_2.dtd">
              <jboss-app>
               <loader-repository>jboss.loader:loader=wai-processor</loader-repository>
              </jboss-app>
              


              • 4. Re: EAR Scoped PersistenceUnit
                mzeijen

                Other strange thing that I discovered is that the /lib/pu.jar explicitly needs to be definded in the application.xml file else the persistence-unit won't be initialized. Other dependencies in this directory get found. Is this also a bug?

                • 5. Re: EAR Scoped PersistenceUnit
                  mzeijen

                  Nobody got any Idea. Should I just register this as a Bug in Jira?

                  • 6. Re: EAR Scoped PersistenceUnit
                    alexg79

                     

                    Other strange thing that I discovered is that the /lib/pu.jar explicitly needs to be definded in the application.xml file else the persistence-unit won't be initialized. Other dependencies in this directory get found. Is this also a bug?

                    No. If you just put your jar in the /lib directory, it doesn't get processed for deployment purposes. All deployables must be listed in application.xml.

                    • 7. Re: EAR Scoped PersistenceUnit
                      wolfc

                       

                      "mzeijen" wrote:
                      Other strange thing that I discovered is that the /lib/pu.jar explicitly needs to be definded in the application.xml file else the persistence-unit won't be initialized. Other dependencies in this directory get found. Is this also a bug?

                      In AS 4.2 anything found in lib is added as a library module (added to the classpath) nothing more. If it represents another type of module it must be added to application.xml.

                      • 8. Re: EAR Scoped PersistenceUnit
                        mzeijen

                        I discovered that in AS 4.2 with a JEE5 application.xml file all normal jars in the Lib process are added to the classpath but the file containing the persistence.xml needs to be added to the application.xml as a java module.

                        I also found a workaround for my Global persistence unit. By using some life cycle interceptor and a custom annotation I inject the persistence context from the JNDI into the persistence manager field. The injector finds the JNDI path of the persistence context via a configuration file that I put on the classpath in lib aEJB3Lib.jar. This way i can reuse my DAOEJB3Lib.jar and on runtime tell it which persistence unit to use.

                        But I still think that it is a bug that persistence units become global. What do you think?

                        • 9. Re: EAR Scoped PersistenceUnit
                          superdev

                          I tested JBoss GA 5 and find that it is not the case in this release. Persistence.xml should NOT be added to the application.xml as a java module, otherwise, there will be errors saying something like "Context already exists with path 'lib/**.jar".

                          • 10. Re: EAR Scoped PersistenceUnit
                            jaikiran

                            That's right. The application.xml should not contain the persistence.xml module. Instead you should place the persistence.xml in the META-INF of the jar. See this for a detailed explanation http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/entityconfig.html

                            • 11. Re: EAR Scoped PersistenceUnit
                              alrubinger

                               

                              EJB 3.0 Persistence Specification, Section 6.2 wrote:
                              A persistence unit is defined by a persistence.xml file. The jar file or directory whose META-INF
                              directory contains the persistence.xml file is termed the root of the persistence unit. In Java EE, the root of a persistence unit may be one of the following:

                              * an EJB-JAR file
                              * the WEB-INF/classes directory of a WAR file[40]
                              * a jar file in the WEB-INF/lib directory of a WAR file
                              * a jar file in the root of the EAR
                              * a jar file in the EAR library directory
                              * an application client jar file


                              S,
                              ALR