动态 JPA 连接
我有一个相当标准的 Java EE6 Web 应用程序,使用 JPA 2 和依赖注入连接到 MySQL 数据库,一切正常。我现在想做的是让这个应用程序与我们在客户端站点安装的其他应用程序的数据库进行交互 - 本质上充当我们其他应用程序安装的单点控制。
我正在努力解决的是如何最好地执行与其他数据库的交互。理想情况下,我想为每次安装创建一个 EntityManager 并使用 JPA 进行交互,但我看不到任何设置方法。例如,我可能对一种应用程序类型进行了 5 次安装(因此也有数据库),并且主控制应用程序在运行时之前不会知道其他安装。这似乎排除了使用 EntityManager 的依赖注入和所有自动事务划分等。替代选项是仅创建一个数据源并手动进行交互。虽然灵活,但这显然需要付出更多努力。
所以,我的问题实际上是如何最好地解决这个问题?
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. 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. 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. 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. While flexible this clearly requires a lot more effort.
So, my question really is how do I best tackle this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也在研究这个,到目前为止我发现了以下博客文章描述了一种方法
http://ayushsuman.blogspot.com/2010 /06/configure-jpa-during-run-time-dynamic.html :
从 persistance.xml 中删除了所有数据库属性
更改了配置entityManager的java文件,在我们的例子中是TestApplication.java
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 :
Removed all your database properties from persistance.xml
Changed your java file where you are configuring entityManager, in our case TestApplication.java