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.
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
and for kitchsink persistence.xml
<!-- If you are running in a production environment, add a managed
data source, the example data source is just for proofs of concept! -->
but neither of example managed beans use code to inject a PU named "loginDatabase" or "primary"
If there is only one persistence unit in the application you can inject it without specifying the name
ok, that does help a bit,
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
and so does kitchensink web app, basically the same
each web app's example code injects an EntityManager like this
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
if i come along and add another datasource to standalone.xml for my web app, i need to use this though correct?
i am going to go reread the java ee 6 tutorial and see if it says anything :-)
For each separate datasource used in your application , there should be a separate persistence unit for that datasource.
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.
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
1 of 1 people found this helpful
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 , thanks, very helpful, now i understand what is going on
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).