如何在JPA中使用多个数据源 [英] How to use more than one data sources in JPA
本文介绍了如何在JPA中使用多个数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想同时使用两个数据源,并根据情况使用它们. 我可以在 persistence.xml 中添加多个数据源吗?
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:
<jta-data-source>gov/v3/jdbc/dataSource1</jta-data-source>
<jta-data-source>gov/v3/jdbc/dataSource2</jta-data-source>
请发送您的建议.
注意:应用程序服务器是weblogic 10.3
Note: Application server is weblogic 10.3
推荐答案
您可以通过以下方式在 persistence.xml 中配置多个数据源:
You can configure multiple datasources in your persistence.xml this way:
<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();
这篇关于如何在JPA中使用多个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文