2017-11-13 4 views
0

私は、地理的に異なる場所にある多くのデータベースを使用するアプリケーションを持っています。すべてのデータベースには同じテーブルがあり、データベース名だけが場所によって異なります。自分のアプリケーションで、各データベースのデータを使用するいくつかの統計および監視チャートを作成する必要があります。J2EE - JPAの異なるデータベースに対する複数のデータベース接続

JSFとJPAを使用してWEB Javaアプリケーションからこれらのデータベース接続を作成する適切な方法は何でしょうか。

答えて

2

あなたpersistence.xmlファイルにより永続ユニットを定義することができます:あなたはTomEEまたはコンテナ(Tomcatの)のような完全なアプリケーションサーバを使用している場合、今、それが依存

<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> 

。それ以外の場合は

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

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

Persistence.createEntityManagerFactory("pu1"); 
Persistence.createEntityManagerFactory("pu2"); 
関連する問題