简体   繁体   English

动态JPA连接

[英]Dynamic JPA Connection

I have a fairly standard Java EE6 web application using JPA 2 with dependency injection connecting to a MySQL database and everything is working fine. 我有一个相当标准的Java EE6 Web应用程序使用JPA 2,依赖注入连接到MySQL数据库,一切正常。 What I would like to do now is have this application interact with the databases of other applications we have installed at a clients site - essentially acting as a single point of control for our other application installs. 我现在要做的是让这个应用程序与我们在客户端站点安装的其他应用程序的数据库进行交互 - 基本上作为我们其他应用程序安装的单一控制点。

What I'm struggling with is how best to perform the interaction with the other databases. 我正在努力的是如何最好地与其他数据库进行交互。 Ideally I would like to create an EntityManager for each install and interact using JPA but I can't see any way to set this up. 理想情况下,我想为每次安装创建一个EntityManager并使用JPA进行交互,但我看不到任何设置方法。 I may, for example, have 5 installs (and therefore databases) of one application type and the master control application won't know about the other installs until runtime. 例如,我可能有一个应用程序类型的5个安装(以及数据库),并且主控制应用程序在运行时之前不会知道其他安装。 This seems to preclude using dependency injection of an EntityManager and all the automatic transaction demacation etc etc. The alternative option is to just create a DataSource and do the interactions manually. 这似乎排除了使用EntityManager的依赖注入和所有自动事务demacation等等。替代选项是只创建一个DataSource并手动进行交互。 While flexible this clearly requires a lot more effort. 虽然灵活,但这显然需要更多的努力。

So, my question really is how do I best tackle this problem? 所以,我的问题是如何最好地解决这个问题?

I'm also looking into this, and so far I have found the following blog post that describes a way to do it http://ayushsuman.blogspot.com/2010/06/configure-jpa-during-run-time-dynamic.html : 我也在研究这个问题,到目前为止,我发现以下博客文章描述了一种方法来做到这一点http://ayushsuman.blogspot.com/2010/06/configure-jpa-during-run-time-dynamic .html

Removed all your database properties from persistance.xml 从persistance.xml中删除了所有数据库属性

<persistence>
<persistence-unit name="jpablogPUnit" transaction-type="RESOURCE_LOCAL">
<class>com.suman.Company</class>
</persistence-unit>
</persistence>

Changed your java file where you are configuring entityManager, in our case TestApplication.java 更改了您在配置entityManager的java文件,在我们的示例中为TestApplication.java

package com.suman;

import java.util.HashMap;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.log4j.Logger;

/**
* @author Binod Suman
*/
public class TestApplication {

Logger log = Logger.getLogger(TestApplication.class);
public static void main(String[] args) {
TestApplication test = new TestApplication();
test.saveCompany();
}

public void saveCompany(){
log.info("Company data is going to save");
EntityManagerFactory emf;
Map properties = new HashMap();
properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
properties.put("hibernate.connection.url", "jdbc:mysql://localhost:3306/sumandb");
properties.put("hibernate.connection.username", "root");
properties.put("hibernate.connection.password", "mysql");
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
properties.put("hibernate.show-sql", "true");
//emf = Persistence.createEntityManagerFactory("jpablogPUnit");
emf = Persistence.createEntityManagerFactory("jpablogPUnit",properties);
EntityManager entityManager = (EntityManager) emf.createEntityManager();
entityManager.getTransaction().begin();
Company company = new Company(120,"TecnoTree","Espoo, Finland");
entityManager.persist(company);
entityManager.getTransaction().commit();
log.info("Company data has been saved");
}

}

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

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