1 Reply Latest reply on Jan 27, 2010 5:19 PM by Thomas Goettlich

    2 EARs with the same persistence unit

    Thomas Goettlich Novice

      Hi all,


      we have a problem with our 'application' that is split into two isolated EARs and deployed in JBoss AS 4.2.3.GA.

      Both EARs need some configuration and thus they both contain a configuration.jar that contains entities, DAOs and services.

      The DAO gets an entitymanager injected by use of the @PersistenceContext(unitName="config") annotation.

      Each EAR contains its own persistence.xml which defines the persistence unit "config".


      If we deploy one ear, everything works fine, but if we deploy both, we get ClassCastExceptions.


      Both persistence units are deployed in the global JNDI namespace:





      It seems that the EARwhich is deployed second gets a reference to the persistence unit defined in the other EAR, i.e. ear-two.ear gets a reference to the first "config" persistence unit.


      Is there a way to separate the persistence units?

      I guess they should go into the java:comp/ namespace instead. But how would I do this?


      Any help is highly appreciated.


      Thanks in advance,



        • 1. Re: 2 EARs with the same persistence unit
          Thomas Goettlich Novice

          Never mind, I found the reason and a solution/workaround.


          Seems like JBoss 4.2.3 injects an entity manager from the first found persistence unit with the given name.

          Since the persistence units are stored in a hashmap, even renaming our second application broke it (it worked before, but after encountering the problem we wondered why).


          So I applied the fix described in JBAS-5043 and it is working now (I'm not sure the fix is 100% correct though, since the code is too complex to fully understand it in just a couple of hours).


          I also tested our app with JBoss 5.1.0 (after some refactoring, mainly du to changed packages). However, there still seems to be a problem with two persistence units having the same name, since the apps were deployed and worked correctly but during deployment the server complained about the PersistenceUnitMetadata for persistence unit 'xy' already existing ('xy' is the name of the two persistence units in different ears).