-
1. Re: jboss7 quickstarts entitymanager injection question
swd847 Nov 7, 2011 4:31 PM (in response to robertwalker)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
robertwalker Nov 7, 2011 4:50 PM (in response to swd847)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
swd847 Nov 7, 2011 4:51 PM (in response to robertwalker)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
robertwalker Nov 8, 2011 1:37 AM (in response to swd847)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
smarlow Nov 8, 2011 5:17 PM (in response to robertwalker)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
robertwalker Nov 8, 2011 7:33 PM (in response to smarlow)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
smarlow Nov 8, 2011 8:55 PM (in response to robertwalker)1 of 1 people found this helpfulRobert,
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
-
8. Re: jboss7 quickstarts entitymanager injection question
robertwalker Nov 8, 2011 9:53 PM (in response to smarlow)scott , thanks, very helpful, now i understand what is going on
-
9. Re: jboss7 quickstarts entitymanager injection question
smarlow Nov 12, 2011 10:13 AM (in response to robertwalker)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