3 Replies Latest reply on Apr 9, 2010 2:12 PM by Andrig Miller

    Multiple Persistence Units with the same entities

    Andrig Miller Apprentice

      In developing some code, it dawned on me that I had some batch processing, and some OLTP process, and that I really wanted to configure them differently, from a JPA perspective.

       

      So, I created two persistence units in my persistence.xml, with two different sets of Hibernate properties, pointing at the same database, and let it default to scanning for the Entities in my jar.

       

      I also then updated my stateless session beans to have two entity managers injected one, referencing one persistent unit name, and the other referencing the other.

       

      In the batch processing methods I use the entity manager that is configured for batch processing, and in the other methods I use the one configured for OLTP processing.

       

      This is all working, but when I was reading various sources for whether this "should" work, it wasn't clear.

       

      So, while it does work, what I am wondering, is this spec compliant, or is this just dumb, blind luck that it works?

        • 1. Re: Multiple Persistence Units with the same entities
          jaikiran pai Master

          Andrig Miller wrote:

           

          In the batch processing methods I use the entity manager that is configured for batch processing, and in the other methods I use the one configured for OLTP processing.

           

          This is all working, but when I was reading various sources for whether this "should" work, it wasn't clear.

           

          So, while it does work, what I am wondering, is this spec compliant, or is this just dumb, blind luck that it works?

          It's definitely not luck I don't see anything wrong in what you are doing.

           

          One thing that you might want to keep note is to make sure that  you don't end up using both those entity managers  in the same transaction - in which case you would be enrolling 2 transaction aware resources (the datasource corresponding to the entity managers) in one transaction. You would then require XA datasources for such cases.

          1 of 1 people found this helpful
          • 2. Re: Multiple Persistence Units with the same entities
            jaikiran pai Master

            Andrig Miller wrote:

             

             

            So, I created two persistence units in my persistence.xml, with two different sets of Hibernate properties, pointing at the same database, and let it default to scanning for the Entities in my jar.

             

            I missed this earlier. So effectively, you are sharing the same entities between multiple entity managers. Do those entity managers really "share" those entities in your application? For example, if you try to access an entity which is shared (i.e. you access the entity using both the entity managers) then you will end up running into issues because each of these entity managers, that come from different entity manager factories, will (possibly) hold a different state.

            • 3. Re: Multiple Persistence Units with the same entities
              Andrig Miller Apprentice

              jaikiran pai wrote:

               

              Andrig Miller wrote:

               

               

              So, I created two persistence units in my persistence.xml, with two different sets of Hibernate properties, pointing at the same database, and let it default to scanning for the Entities in my jar.

               

              I missed this earlier. So effectively, you are sharing the same entities between multiple entity managers. Do those entity managers really "share" those entities in your application? For example, if you try to access an entity which is shared (i.e. you access the entity using both the entity managers) then you will end up running into issues because each of these entity managers, that come from different entity manager factories, will (possibly) hold a different state.

              Well, I don't use the same entities from two different entity manager's at the same time, or within the same transaction.  One entity manager is used to do an initial data load of a bunch of reference data into the database, and the other entity manager is used for transaction processing against that data.  The two processes are independent, and the OLTP processing cannot occur until after the batch process has completed anyway.  So, there is no overlap, where the same entity would be used in two entity managers at the same time, and have different state.

               

              Before even doing this, I thought through that, but is this something that is compliant with the spec.  All the reading I have done, only vaguely hint at having the same entities into two different persistence units, and everything I have read basically says that a persistence unit is tied to one data source (maybe implying that multiple persistence units should have "unique" data sources).

               

              So, while it works now, and based on the functions involved, there is no overlap, will this continue to work in the future?  If its not supported by the spec, then I have a reasonable expectation that this might break in the future.