We have (perhaps?) an unusual problem at work where we're currently trying to work out now that we've decided to rebuild our web application using Spring.
In our design we have one master database and one database for each customer. This design can not be changed (and we don't want to either). Obviously, the master database can be configured in Spring and injected to any class needing it since its configuration parameters are static.
However, the clients' databases are not. Sure, their databases all happen to reside on the same server, but the actual database names differ.
When a user logs in, we associate with the user's session an object called SecureSession containing among other things information about user including which client he/she belongs to. Using the information in this SecureSession, we open the connection when needed to the client's database in our current design.
This client connection, is it possible to somehow have it automatically dependency injected to any classes needing it?
We have a number of DAO-objects which we use to talk to the database(s). A DAO-object will need to speak to both the master database and the client database for the user which made the current request.
Ideally, I see the solution looking something like this.
=> User makes a request
=> If not logged in, send to login page, otherwise continue
=> Request is dispatched to appropiate controller
=> Controllers are configured to have DAOs dependency injected into them.
=> The DAOs have been given the master connection by Spring
=> Using the information in the request's SecureSession, the DAO's are also given a connection to the client's database
So when methods are called in the DAO, you don't have to worry about where the two connections came from, they're just there.
Is it possible to solve the last step above in an elegant manner with Spring? I'm hoping that their perhaps is some kind of interface once can implement and then configure to make the request dependent connection association.
Anyone have any tips? Possible work-arounds?
Thanks in advance.