简体   繁体   English

使用Spring与多个数据源JNDI条目动态关联

[英]Dynamic association with multiple datasource JNDI entries with Spring

I have an existing simple Spring application with a DAO class that extends NamedParameterJdbcDaoSupport . 我有一个现有的简单Spring应用程序,带有DAO类,该类扩展了NamedParameterJdbcDaoSupport

In the Spring context file, I have a "p:dataSource-ref" attribute that refers to a "jee:jndi-lookup" element, which references the JNDI name defined in the container. 在Spring上下文文件中,我有一个“ p:dataSource-ref”属性,它引用一个“ jee:jndi-lookup”元素,该元素引用了在容器中定义的JNDI名称。

Something like this: 像这样:

<jee:jndi-lookup jndi-name="${jndiPrefix}/specificdb" id="mydb"/>
<bean class="com...dao.OrdersDAO" p:name="ordersDAO" p:dataSource-ref="mydb"/>

This all works fine. 这一切都很好。 I've deployed this to both WebLogic and TomEE successfully. 我已经成功地将此部署到了WebLogic和TomEE。

I now need to consider a new application that has a slightly more dynamic relationship with its datasources. 现在,我需要考虑一个新的应用程序,该应用程序与其数据源之间的关系稍微有些动态。 The overall dataflow of the new application will be very similar to the existing application. 新应用程序的整体数据流将与现有应用程序非常相似。

The new application will have N datasources, all with the same data model. 新的应用程序将具有N个数据源,所有数据源都具有相同的数据模型。 The container will have definitions for the JNDI names for all of the datasources, and I imagine I'll have to have an additional JNDI name that just contains a comma-separated string containing all of the JNDI names for the datasources. 容器将具有所有数据源的JNDI名称的定义,我想我将不得不拥有一个附加的JNDI名称,该名称仅包含一个逗号分隔的字符串,该字符串包含数据源的所有JNDI名称。

What are some effective strategies for doing something like this in Spring? 在春季做这样的事情有哪些有效的策略?

Update : 更新

One thing I didn't make clear in my original post is what kind of algorithm I would be using to "select" which datasource to use under the covers. 我在原始帖子中没有明确说明的一件事是,我将使用哪种算法来“选择”要使用的数据源。

The point is, I'm not selecting a single datasource, I'm using all of them. 关键是,我没有选择单个数据源,而是使用了所有数据源。 The application requires the query to be run on all of the datasources and the resultset is the union of all of them. 该应用程序要求查询要在所有数据源上运行,并且结果集是所有数据源的并集。

The first answer describes a strategy that essentially connects a "facade" datasource to one of a set of other datasources. 第一个答案描述了一种策略,该策略实际上将“外观”数据源连接到一组其他数据源中的一个。 That's an interesting problem, but not what I'm looking for here. 这是一个有趣的问题,但不是我在这里寻找的问题。

In any case, I believe I understand how to do this, but I've yet to implement it yet. 无论如何,我相信我知道如何做到这一点,但是我还没有实现它。 My intention is to dynamically create the set of datasources and associate them with a DAO (not sure whether I want a single DAO with a set of datasources or a 1-1 relationship between DAOs and datasources). 我的意图是动态创建一组数据源,并将它们与DAO关联(不确定我是否要使用具有一组数据源的单个DAO或DAO与数据源之间的1-1关系)。 An encompassing service will read a JNDI environment string at startup that contains a list of JNDI names for datasources, and dynamically create the DataSource objects. 包含服务将在启动时读取JNDI环境字符串,其中包含数据源的JNDI名称列表,并动态创建DataSource对象。 The query process will simply collect the results from each DataSource and union them together. 查询过程将简单地从每个数据源收集结果并将它们合并在一起。

TomEE has the same kind of routing capabilities than Spring. TomEE具有与Spring相同的路由功能。

check these links out: 检查这些链接:

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

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