how to manage connections to dynamically created databases

I need to manage connections to multiple databases in my web app. following are facts regarding the current implementation:

1- I use Tomcat

2- databases are created dynamically at runtime ( i am using mysql)

without a doubt, having a connection pool to manage database connections is optimal.
Since the databases are not known at the start of the application, it was not possible for me to set up datasources and make connection pools. (I could not find a way in Tomcat to make dynamic connection pool: a connection pool that is created at runtime).

my question is: what other options do I have to work efficiently with connections to multiple databases ? (I don’t have experience to implement connection pools myself)
is there any library that can be used with tomcat and allow me to establish multiple connection pools to different databases at runtime ? if not what do you suggest that I do instead of connection pools ?
i am fairly new with this issue therefore please correct and guide me if I am messing up concepts.

Thank you in advance.


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The MySQL JDBC driver allows omitting the database name from the connection URL as follows:


You only need to specify the database by Connection#setCatalog() or directly in the SQL queries. See also its reference documentation:

If the database is not specified, the connection will be made with no default database. In this case, you will need to either call the setCatalog() method on the Connection instance or fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Not specifying the database to use upon connection is generally only useful when building tools that work with multiple databases, such as GUI database managers.

This allows you for creating a single and reuseable connection pooled datasource in Tomcat. You’ll perhaps only need to rewrite your connection manager and/or SQL queries.

Method 2

There are enough connection pooling framework in the open. Proxool is definitely among the best. Its pretty flexible and easy to use.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x