简体   繁体   English

如何动态管理多个数据源

[英]How to dynamically manage multiple datasources

Similar topics have been covered in other threads, but I couldn't find a definitive solution to my problem.其他主题中已经涵盖了类似的主题,但我找不到解决问题的明确方法。

What we're trying to achieve is to design a web app which is able to:我们想要实现的是设计一个 web 应用程序,它能够:

  1. read a datasource configuration at startup (an XML file containing multiple datasource definitions, which is placed outside the WAR file and it's not the application-context or hibernate configuration file)在启动时读取数据源配置(包含多个数据源定义的 XML 文件,该文件位于 WAR 文件之外,它不是应用程序上下文或 hibernate 配置文件)
  2. create a session factory for each one of them (considering that each datasource is a database with a different schema)为它们中的每一个创建一个 session 工厂(考虑到每个数据源都是具有不同模式的数据库)
  3. switch at runtime to different datasources based on user input (users can select which datasource they want to use)在运行时根据用户输入切换到不同的数据源(用户可以 select 他们想使用哪个数据源)
  4. provide the correct dao object to manage user requests.提供正确的 dao object 来管理用户请求。

At the moment we have a DAO Manager object which is able to read the datasource configuration file and instantiate multiple session factories, saving them in a map.目前我们有一个 DAO 管理器 object,它能够读取数据源配置文件并实例化多个 session 工厂,将它们保存在 map 中。 Each session factory is created with a configuration containing the proper hibernate mapping classes (different for each database schema).每个 session 工厂都是使用包含正确 hibernate 映射类(每个数据库模式不同)的配置创建的。 Moreover we have multiple DAO interfaces with their implementations, used to access "their database".此外,我们有多个 DAO 接口及其实现,用于访问“他们的数据库”。

At this point we would need a way to get from the DAO Manager a specific DAO object, containing the right session factory attached, all based on the user request (basically a call from the above service containing the datasource id or a custom datasource object).此时,我们需要一种方法从 DAO 管理器获取特定的 DAO object,其中包含附加的正确 session 工厂,所有这些都基于用户请求(基本上是来自上述服务的调用,包含数据源 ID 或自定义数据源对象) .

Ideally the service layer should use the DAO Manager to get a DAO object based on the datasource id (for instance), without worrying about it's actual implementation: the DAO Manager would take care of it, by creating the correct DAO object and injecting in it the right session factory, based on the datasource id.理想情况下,服务层应该使用 DAO 管理器来获取基于数据源 ID(例如)的 DAO object,而不用担心它的实际实现:DAO 管理器会通过创建正确的 DAO object 并注入它来处理它正确的 session 工厂,基于数据源 id。

My questions are:我的问题是:

  • Is this a good approach to follow?这是一个很好的方法吗?
  • How can I use Spring to dynamically inject in the DAO Manager multiple DAO implementations for each DAO interface?如何使用 Spring 在 DAO 管理器中为每个 DAO 接口动态注入多个 DAO 实现?
  • Once the session factories are created, is there a way to let Spring handle them, as I would normally do with dependency injection inside the application-context.xml?一旦创建了 session 工厂,有没有办法让 Spring 处理它们,就像我通常在 application-context.xml 中进行依赖注入一样?
  • Would the 2nd Level Cache still work for each Session Factory?二级缓存是否仍然适用于每个 Session 工厂?

Is this a good approach to follow?这是一个很好的方法吗?

It's probably the only possible approach.这可能是唯一可能的方法。 So, yes.所以,是的。

How can I use Spring to dynamically inject in the DAO Manager multiple DAO implementations for each DAO interface?如何使用 Spring 在 DAO 管理器中为每个 DAO 接口动态注入多个 DAO 实现?

Dynamically?动态的? I thought you wanted to do it at startup time.我以为你想在启动时这样做。 If so, just provide an accessor with a list or array:如果是这样,只需提供一个带有列表或数组的访问器:

public void setMyDaos(List<Mydao> daos){
     this.daos = daos;
}

Once the session factories are created, is there a way to let Spring handle them, as I would normally do with dependency injection inside the application-context.xml?一旦创建了 session 工厂,有没有办法让 Spring 处理它们,就像我通常在 application-context.xml 中进行依赖注入一样?

This one's tough.这个好难I'd say you will probably have to store your sessionFactory bean in scope=session我会说你可能不得不将你的 sessionFactory bean 存储在scope=session

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

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