简体   繁体   中英

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:

  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)
  2. create a session factory for each one of them (considering that each datasource is a database with a different schema)
  3. switch at runtime to different datasources based on user input (users can select which datasource they want to use)
  4. provide the correct dao object to manage user requests.

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. Each session factory is created with a configuration containing the proper hibernate mapping classes (different for each database schema). Moreover we have multiple DAO interfaces with their implementations, used to access "their database".

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).

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.

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?
  • 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?
  • Would the 2nd Level Cache still work for each Session Factory?

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?

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?

This one's tough.I'd say you will probably have to store your sessionFactory bean in scope=session

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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