This content has been marked as final.
Show 6 replies
-
1. Re: How to add multiple entityNamager in one war?
garyyding Mar 8, 2009 3:11 AM (in response to garyyding)Any people can give some sugestion?
On one war or web application, it is easy to use one entityMaager to connect one database.But if I want connect multiple database, how I define multiple entityManager, and then injected into my service class?When I define multiple entityManager in components.xml file, it will issue error: found same components. Why?
-
2. Re: How to add multiple entityNamager in one war?
swd847 Mar 8, 2009 11:01 PM (in response to garyyding)Declare the two entityManagers in persistence.xml, then declare them in components.xml with different names.
-
3. Re: How to add multiple entityNamager in one war?
garyyding Mar 9, 2009 10:44 AM (in response to garyyding)My code is
<code>
EntityManager em2 = (EntityManager)getInstance("em2");
EntityManager em1 = (EntityManager)getInstance("em");
User user = new User("test11","test11","test11");
em1.persist(user);
em2.persist(user);
</code>
But I found it is ok for "em1.persist(user);"
but fail for "em2.persist(user);". When I call it, nothing happened. No database operation from console, no data saved into database.
Please help to give some suggestion. Thanks.
Please refer to detailed configuration
1. in components.xml
<code>
<transaction:entity-transaction entity-manager="#{em}" />
<persistence:entity-manager-factory name="wfwcoredatasource" persistence-unit-name="wfwcoredatasource"/>
<persistence:managed-persistence-context name="em"
auto-create="true" entity-manager-factory="#{wfwcoredatasource}" />
<transaction:entity-transaction name="tt2" entity-manager="#{em2}"/>
<persistence:entity-manager-factory name="wfwcoredatasource2" persistence-unit-name="wfwcoredatasource2"/>
<persistence:managed-persistence-context name="em2"
auto-create="true" entity-manager-factory="#{wfwcoredatasource2}" />
</code>
2. in persistence.xml
<code>
<persistence-unit name="wfwcoredatasource"
transaction-type="RESOURCE_LOCAL">
.....
</persistence-unit>
<persistence-unit name="wfwcoredatasource2"
transaction-type="RESOURCE_LOCAL">
.....
</persistence-unit>
</code> -
4. Re: How to add multiple entityNamager in one war?
garyyding Mar 9, 2009 10:58 AM (in response to garyyding)It seems that I am wrong to set/use the seam transaction.
-
5. Re: How to add multiple entityNamager in one war?
garyyding Mar 9, 2009 11:18 AM (in response to garyyding)If I write the following code, it is ok
em2.getTransaction().begin();
em2.persist(u);
em2.getTransaction().commit();But I don't want it.
-
6. Re: How to add multiple entityNamager in one war?
swd847 Mar 10, 2009 8:50 AM (in response to garyyding)I take it you do not have the option of using JTA transactions rather than RESOURCE_LOCAL?