PDA

View Full Version : Dynamic Datasources/Daos



davidcnoel
Nov 17th, 2005, 12:51 PM
An application I am starting to work on requires that I am able to connect to many different databases. I plan on using the standard Dao pattern but my problem is that I will not know which databases I am supposed to talk to until runtime. This will mean that I cannot configure my datasources in the usual fashion in a spring context. Is there any standard pattern to follow to solve this problem? I was thinking I would create a factory type of object that would manage multiple datasources. Basically you would request a datasource for a database with the name 'A' and it would create the datasource if it did not exist or return a reference to a datasource from the "pool" if it did exist. Even If I do this I still have a problem that the existing DaoSupport classes that spring provides only work with a single datasource. So I would think I would need some type of factory that would create a new instance of my Dao object for the particular datasource I want to talk to. I'm also going to want to take advantage of transactions through Spring AOP so I need to make sure I consider this as well.

I'm thinking this is a common issue so I hope there is an existing best practice or pattern to follow.

Any ideas?

Thanks

Dave

Alarmnummer
Nov 20th, 2005, 02:40 AM
Check this topic:
http://forum.springframework.org/showthread.php?t=19806

If you create some kind of factory that returns your desired objects (or an environment with all desired objects) you have runtime access to your dependencies.

rgeorgiev
Nov 21st, 2005, 05:05 AM
Do you have some standart criteria for connecting to different databases?
I so, look at <code>org.springframework.aop.target.HotSwappableTargetS ource</code>

davidcnoel
Dec 3rd, 2005, 11:16 AM
using a HotSwappableTargetSource with a proxy datasource worked. Thanks for the advice.

barinek
Mar 17th, 2006, 02:31 PM
any chance you could post the java source?