开发者

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: wha开发者_开发知识库t 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.


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

jdbc:mysql://localhost:3306

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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜