9 Replies Latest reply on Nov 12, 2011 10:13 AM by Scott Marlow

    jboss7 quickstarts entitymanager injection question

    robert walker Newbie

      Hi all, I am looking at the examples in jboss7 quickstarts , login and kithensink (ones that hit a DB)

      and wondering how jb7 knows which persistence unit to inject.

       

      for example

       

      if standalone.xml defines a few datasources  (like the default)

       

      <datasource jndi-name="java:jboss/datasources/ExampleDS" pool-name="H2DS" enabled="true" jta="true" use-java-context="true" use-ccm="true">

       

      and say another named  "MyDataDSource"

       

      <datasource jndi-name="java:jboss/datasources/MyDataDSource" pool-name="MyPool" enabled="true" jta="true" use-java-context="true" use-ccm="true">

       

      how does jboss know which one to inject when executing code like this from login quickstart's   ManagedBeanUserManager.java file

       

      @Inject

      private EntityManager userDatabase;

       

       

      No designation which PU to to inject,  I was thinking there would have to be code along the idea of the snippet below

       

      @PersistenceContext(unitName = "MyDataDSource")

      EntityManager em;

       

       

      I did read the "Chapter 5. CDI + JPA + EJB + JTA + JSF: Login quickstart" but was eagerly wanting to know how jboss knows which

      one to inject but didn't see it, which means it's so obvious that it does not need to be stated. So I realize i should know it this

      but I don''t. :-(

       

      I see the the pattern suggested by CDI is resource producers, but

      Resources.java has

       

         // Expose an entity manager using the resource producer pattern

         @SuppressWarnings("unused")

         @PersistenceContext

         @Produces

         private EntityManager em;

       

      with no designation either

       

      can someone point me to a reesource to explain this a bit more

       

      thanks

        • 1. Re: jboss7 quickstarts entitymanager injection question
          Stuart Douglas Master

          Persistence units (i.e. EntityManagers) are defined in persistence.xml, having a datasource present does not automatically create a corresponding EntityManager. The connection to use is specified in persistence.xml.

          • 2. Re: jboss7 quickstarts entitymanager injection question
            robert walker Newbie

            thanks for the reply

             

            i must be missing someting, each example, login or kitchensink has there own persistence.xml file

            which gives the PU a name, which i would think would be needed by the class/code that is doing the injecting  of the EntityManager.

             

            for login's  persistence.xml

             

            <persistence-unit name="loginDatabase">
                  <provider>org.hibernate.ejb.HibernatePersistence</provider>
                  <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>

             

            and for kitchsink  persistence.xml

             

            <persistence-unit name="primary">
                  <!-- If you are running in a production environment, add a managed
                     data source, the example data source is just for proofs of concept! -->
                  <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>

             

            but neither of example managed beans use code to inject a PU named "loginDatabase" or "primary"

            they simply

             

            @Inject

            EntityManager em;

             

             

            • 3. Re: jboss7 quickstarts entitymanager injection question
              Stuart Douglas Master

              If there is only one persistence unit in the application you can inject it without specifying the name

              • 4. Re: jboss7 quickstarts entitymanager injection question
                robert walker Newbie

                ok, that does help a bit,

                 

                however

                 

                standalone.xml defines a single datasource named  like such

                 

                <datasource jndi-name="java:jboss/datasources/ExampleDS" ...

                 

                now the login web application defines a persistence.xml

                 

                <persistence-unit name="loginDatabase">     
                      <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>

                 

                and so does kitchensink web app, basically the same

                 

                persistence-unit name="primary">
                      <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>

                 

                 

                each web app's example code injects an EntityManager like this

                 

                @Inject

                EntityManager em

                 

                so i guess   "java:jboss/datasources/ExampleDS"   string is being used

                to get to the datasource as opposed to doing something more conventional like this

                 

                @PersistenceContex(unitName="loginDatabase") // get EntityManager for login web application

                EntityManger em

                 

                if i come along and add another datasource to standalone.xml for my web app, i need to use this though correct?

                 

                @PersistenceContex(unitName="

                EntityManger em

                 

                 

                i am going to go reread the java ee 6 tutorial and see if it says anything :-)

                 

                 

                thanks

                 

                • 5. Re: jboss7 quickstarts entitymanager injection question
                  Scott Marlow Master

                  Robert,

                   

                  For each separate datasource used in your application , there should be a separate persistence unit for that datasource. 

                  @PersistenceContext(unitName="PersistenceUnitForMyEmployeeDatasource"

                  EntityManager emEmployee;

                   

                  @PersistenceContext(unitName="PersistenceUnitForMyAccountsDatasource"

                  EntityManager emAccounts;

                   

                   

                  When you use the above emEmployee, you will be creating/retrieving/updating/deleting employee datasource related entities.  When you use the above emAccounts, you will be dealing with the accounts datasource. 

                   

                  Currently, if you were to leave the unit name out (for either EntityManager), the first persistence unit definition found (in the application), will be used.  There isn't any magic to make the right choice, between the two separate persistence unit definitions.  If you have an application with only one datasource and persistence unit definition, you could leave the UnitName blank. 

                   

                  In my opinion, a best practice, is to always specify the unitName, as that will make it easier in the future, when you go from one Datasource to multiple.  I always want to make the code easy to maintain for the future generation.  Of course there are probably many applications that will always have just one Datasource and those applications are fine to leave the unitName blank.

                  • 6. Re: jboss7 quickstarts entitymanager injection question
                    robert walker Newbie

                    scott, u mention

                     

                    >For each separate datasource used in your application , there should be a separate persistence unit for that datasource

                     

                    I understand this, but what i am confused about is these are 2 different applications, jboss 7 quickstarts login and kitchensink

                    yet each one's persistence.xml points to the same data source, and I guess that is ok, what if both applications are deployed on the server

                    running at same time, i guess it is just odd to me they are sharing the same DS, an in memory H2 database/source

                     

                    • 7. Re: jboss7 quickstarts entitymanager injection question
                      Scott Marlow Master

                      Robert,

                       

                      That is really an application level concern, whether each application has its own datasource or uses separate datasources.  If it helps, think of the data source, in terms of database server connections.  Each data source is capable of handing out a number of database connections, that are used by the JPA persistence provider to create/read/update/delete entities. 

                       

                      The H2 (http://en.wikipedia.org/wiki/H2_%28DBMS%29) database and other database engines, are capable of performing several operations at the same time, each occuring on a separate database connection.

                       

                      If two separate applications are sharing the same datasource, that just means they are sharing the database connection pool associated with that datasource.  The applications could also be sharing tables or they could each have there own separate tables on the database.  Sharing tables, between applications is not an attractive idea but its been done.  When sharing tables, you would probably have to avoid caching data in memory (via JPA second level cache) but otherwise, would work fine.  If you can have a separate database per application, that is wonderful but sometimes its not possible. 

                       

                      Scott

                      1 of 1 people found this helpful
                      • 8. Re: jboss7 quickstarts entitymanager injection question
                        robert walker Newbie

                        scott , thanks, very helpful, now i understand what is going on

                        • 9. Re: jboss7 quickstarts entitymanager injection question
                          Scott Marlow Master

                          Robert,

                           

                          I was wondering if you would report the issue that you ran into, with multiple persistence units being used in the quick start application but without specifying the persistence unit name.  If you want to get credit for reporting the bug or even helping to fix them (if you want to), the link to report the bug is https://issues.jboss.org/browse/AS7

                           

                          It might also be worth mentioning that the fix for jira AS7-2275, will prevent applications that have multiple persistence units from using ambigious references to them (unit name will have to be specified for each EntityManager or EntityManagerFactory).

                           

                          Thanks,

                          Scott