简体   繁体   English

如何管理与动态创建的数据库的连接

[英]how to manage connections to dynamically created databases

I need to manage connections to multiple databases in my web app. 我需要在我的Web应用程序中管理与多个数据库的连接。 following are facts regarding the current implementation: 以下是有关当前实施的事实:

1- I use Tomcat 1-我使用Tomcat

2- databases are created dynamically at runtime ( i am using mysql) 在运行时动态创建2-数据库(我使用的是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). (我在Tomcat中找不到创建动态连接池的方法:在运行时创建的连接池)。

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 ? (我没有自己实现连接池的经验)是否有任何可以与tomcat一起使用的库,并允许我在运行时建立到不同数据库的多个连接池? 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: MySQL JDBC驱动程序允许从连接URL中省略数据库名称,如下所示:

jdbc:mysql://localhost:3306

You only need to specify the database by Connection#setCatalog() or directly in the SQL queries. 您只需要通过Connection#setCatalog()或直接在SQL查询中指定数据库。 See also its reference documentation : 另见其参考文档

If the database is not specified, the connection will be made with no default database. 如果未指定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. 在这种情况下,您需要在Connection实例上调用setCatalog()方法,或者在SQL中使用数据库名称(即SELECT dbname.tablename.colname FROM dbname.tablename... )完全指定表名。 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. 不指定在连接时使用的数据库通常仅在构建使用多个数据库(例如GUI数据库管理器)的工具时才有用。

This allows you for creating a single and reuseable connection pooled datasource in Tomcat. 这允许您在Tomcat中创建单个且可重复使用的连接池数据源。 You'll perhaps only need to rewrite your connection manager and/or SQL queries. 您可能只需要重写连接管理器和/或SQL查询。

There are enough connection pooling framework in the open. 在open中有足够的连接池框架。 Proxool is definitely among the best. Proxool绝对是最好的。 Its pretty flexible and easy to use. 它非常灵活且易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM