views:

50

answers:

2

Dear All, I'm building a core java application in which i have say 20 databases and want to connect to each of them depending upon from where request has come to access which database.

I want to connect to databases only once from where i can pick the connection and return; please suggest what type of architecture to follow.

+2  A: 

You have to keep a list of connection pools.

I would use a Map< String, ConnectionPool >, where the key is the connection id. When you need a connection, you take it from this map :

Connection connectionDbAccounts = connectionMap.get("accounts").getConnection();
// Use connectionDbAccounts...
connectionDbAccounts.close();
Benoit Courtine
A: 

Use a HashMap<Location, DatabaseConnection> ?

ced
No, please, don't maintain an open connection, use a DatabaseConnectionpool instead.
helios
Whether to maintain an open connection, use a pool, or open a new connection for each request depends on other things. Which I would not guess from this problem statement.
ced