0

I have an application which uses many databases in different geographical locations. All the databases contains same tables and only the database name is different according to the location. I have to create some statistic and monitoring charts in my application which uses data from each database.

What would be the proper way to create those database connection from a WEB java application with JSF and JPA.

1 Answer 1

2

You can define more persistence units in your persistence.xml file:

<persistence-unit name="pu1" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.url" value=""/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value="/>
</properties>
</persistence-unit>

<persistence-unit name="pu2" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.url" value=""/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="javax.persistence.jdbc.password" value="/>
</properties>
</persistence-unit>

Now it depend if you are using full application server like TomEE or container (Tomcat).

@PersistenceContext(unitName = "pu1")
private EntityManager em;

@PersistenceContext(unitName = "pu2")
private EntityManager em2;

Otherwise:

 Persistence.createEntityManagerFactory("pu1");
 Persistence.createEntityManagerFactory("pu2");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.