简体   繁体   English

在EJB项目中使用Jboss动态加载DataSource

[英]Dynamic loading DataSource with Jboss inside EJB project

lets consider I have one main database with one table. 让我们考虑一个主数据库和一个表。

I created a datasource xml file inside the deploy directory from jboss and put inside the ejb project into the persistance.xml that datasource. 我在jboss的deploy目录中创建了一个datasource xml文件,并将ejb项目放入数据源的persistance.xml中。

Inside my ejb project I am using the @PersistenceContext and load this one table with that entity manager. 在我的ejb项目中,我使用@PersistenceContext并使用该实体管理器加载这一个表。 Inside the table are the some datasource names (the datasource names are jndi datasource names?) I also created in the deploy directory before. 表中有一些数据源名称(数据源名称是jndi数据源名称?)我之前也在deploy目录中创建过。 This bean who loaded the other datasource names now call other ejbs and passes them one of the datasource names from the table. 加载其他数据源名称的bean现在调用其他ejbs并从表中传递一个数据源名称。

This other ejb should now connect to that database with the given datasource name instead of the inside the persistance.xml. 此另一个ejb现在应该使用给定的数据源名称而不是persistance.xml内部连接到该数据库。

In other words: I want to load in one ejb the datasource names. 换句话说:我想在一个ejb中加载数据源名称。 Then I want to call other ejbs and each ejb should connect to a different datasource. 然后我想调用其他ejbs,每个ejb应该连接到不同的数据源。

Thanks a lot, Hauke 非常感谢,Hauke

I'm not quite sure what you're after here, so a word of caution is sounds like you're trying to do something a wee bit insane (the JPA doesn't like dynamics at runtime). 我不太清楚你在这之后会发生什么,所以请注意听起来像是在尝试做一些疯狂的事情(JPA在运行时不喜欢动态)。

However, there are a few options: what I'm hearing is that you want to change the connection to the database. 但是,有几个选项:我听到的是您想要更改与数据库的连接。 First, your persistence XML is only a starting point, you don't have to include a datasource. 首先,您的持久性XML只是一个起点,您不必包含数据源。 This isn't best practice as the idea of the DataSource and datasourse-ds.xml is to make sure you can configure the application without recompiling the code (and multiple deployments etc...). 这不是最佳实践,因为DataSource和datasourse-ds.xml的想法是确保您可以配置应用程序而无需重新编译代码(以及多个部署等...)。 If you take over the job of managing the connection you can use your entity manager. 如果您接管管理连接的工作,您可以使用您的实体经理。

I just want to clarify this is probably a bad idea (you're talking about an ORM framework --JPA Hibernate in this case) but instead of using @PersistenceContext you can provide your own entity manager. 我只是想澄清这可能是一个坏主意(你在谈论ORM框架 - 在这种情况下是JPA Hibernate)但是你可以提供自己的实体管理器,而不是使用@PersistenceContext。 I highly recommend looking into using EclipseLink as a workaround for this. 我强烈建议您使用EclipseLink作为解决方法。 It is not as embedded with JBoss and you'll be able (once it is running) to configure it from the ground up. 它不像JBoss那样嵌入,你可以(一旦它运行)从头开始配置它。

http://wiki.eclipse.org/EclipseLink http://wiki.eclipse.org/EclipseLink

Once you have it running you can actively create and destroy the entity managers, or have numerous entity manager that you create from the properties of a connection. 运行后,您可以主动创建和销毁实体管理器,或者拥有从连接属性创建的众多实体管理器。

The long and short here: I think you're going down a very bad path and you probably want to rethink the design. 这里的长短:我认为你走的路很糟,你可能想重新考虑设计。 The JPA might be the wrong tool, and as much as I hate to say it, you're probably better off with javax.sql than you are with a JPA datasource if you're constantly changing connections. JPA可能是错误的工具,尽管我不愿意这样说,但是如果你不断改变连接,那么使用javax.sql可能比使用JPA数据源更好。

Anyhow, using Eclipselink2.X you could do something like the following -> 无论如何,使用Eclipselink2.X你可以做类似以下的事情 - >

HashMap<String, Object> connectionA new = HashMap<String, Object>();
propsA.put("javax.persistence.jdbc.driver", "driverClass");
propsA.put("javax.persistence.jdbc.url", "connectionString");
. . .
etc...
EntityManager entityManager = Persistence.createEntityManagerFactory("myUnit").
createEntityManager(propsA);

At which point you can create a new set of properties (With a new connection backing it) for each needed manager. 此时,您可以为每个需要的管理器创建一组新属性(使用新连接支持它)。

A nightmare? 一个噩梦? Yes. 是。 Container Managed? 容器管理? No--you'll be in-charge of handling transactions, but should it work? 不 - 你负责处理交易,但它应该有效吗? Absolutely. 绝对。

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

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