简体   繁体   English

如何在JPA中使用多个数据源

[英]How to use more than one data sources in JPA

I want to use two data sources at same time and use them based on the condition. 我想同时使用两个数据源并根据条件使用它们。 Can I add multiple data sources in persistence.xml like: 我可以在persistence.xml中添加多个数据源,如:

<jta-data-source>gov/v3/jdbc/dataSource1</jta-data-source>
<jta-data-source>gov/v3/jdbc/dataSource2</jta-data-source>

Please send your suggestion. 请发送你的建议。

Note: Application server is weblogic 10.3 注意:应用程序服务器是weblogic 10.3

You can configure multiple datasources in your persistence.xml this way: 您可以通过以下方式在persistence.xml中配置多个数据源:

<persistence ...>
  <persistence-unit name="persistence-unit-1">
    <jta-data-source>jdbc/persistence-unit-1</jta-data-source>
    ...
  </persistence-unit>
  <persistence-unit name="persistence-unit-2">
     <jta-data-source>jdbc/persistence-unit-2</jta-data-source>
     ...
  </persistence-unit>
</persistence>

Use these datasources from your application as follows: 使用应用程序中的这些数据源,如下所示:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("persistence-unit-1");
EntityManager em = emf.createEntityManager();

Based on what "condition"? 基于什么“条件”?

EclipseLink does support data partitioning, not sure if this is what you are looking for. EclipseLink确实支持数据分区,不确定这是否是您正在寻找的。

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Data_Partitioning http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Data_Partitioning

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM