I work on a project where the design is to have a database per customer. I have managed to read in Tomcat documentation and establish a connection pool. Since the databases are created automatically by the application when a customer registers, I needed a way to make a dynamic pool. the following post on stack overflow gave the hint of what is required:
Using dynamic Datasource with Tomcat
and I found on the web a page showing how to do this:
Writing and using a Tomcat ObjectFactory
I still have few questions:
1- who takes care of managing the connections in the pool ? in the datasource declaration in context.xml, we define some parameters that control various aspects like max idle time and max number of connections, etc. does tomcat handle this or do I have to take care of it in the implementation that i make when coding a class that implements the ObjectFactory interface ?
2- As I understand, when connections are closed, they return to the pool. suppose that I make a dynamic pool that has maximum 20 connections. and then 30 customers log in. so the pool returns 20 connections for the first 20 customers. when those connections are closed, they return to the pool and the next 10 customers will ask for connections to different databases than the ones already found in the pool. and the question is: will the pool automatically drop 10 connections and add new ones with the correct databases ? in if this is the case, does this really have an advantage over opening a connection upon request without a pool ? (asked with the thought of a site that will be heavily loaded. the amount of connection drop if i guessed correctly might be very high that it wouldn't justify what the pool is there to do: minimize creating connections from scratch)
3- I also wanted to ask if it is ok to implement it myself (considering I am new to this) or if i should find a library that does this (in case its not a task for the beginner in this area).
4- are there any alternatives for connection pools for managing connections to multiple databases that are added dynamically in an efficient way? (in case datasource will not help) what are the approaches used in similar situations?
Thank you in advance for your effort.