如何管理动态创建的数据库的连接 [英] how to manage connections to dynamically created databases

查看:113
本文介绍了如何管理动态创建的数据库的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的网络应用中管理与多个数据库的连接。以下是有关当前实施的事实:

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

1-我使用Tomcat

1- I use Tomcat

2-数据库是动态创建的运行时(我正在使用mysql)

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

毫无疑问,拥有一个连接池来管理数据库连接是最佳选择。
由于数据库在应用程序启动时未知,因此我无法设置数据源并建立连接池。 (我在Tomcat中找不到建立动态连接池的方法:在运行时创建的连接池。)

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一起使用的库,并允许我在运行时建立到不同数据库的多个连接池?如果不是,你建议我做什么而不是连接池?
i这个问题相当新,所以如果我弄乱了概念,请纠正并指导我。

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.

提前谢谢你。

推荐答案

MySQL JDBC驱动程序允许从连接URL中省略数据库名称,如下所示:

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

jdbc:mysql://localhost:3306

您只需要通过 连接#setCatalog() 或直接在SQL查询中。另请参阅参考文档


如果未指定数据库,则将建立没有默认数据库的连接。在这种情况下,您需要在 Connection 实例上调用 setCatalog()方法,或者完全指定表名在SQL中使用数据库名称(即 SELECT dbname.tablename.colname FROM dbname.tablename ... )。不指定在连接时使用的数据库通常仅在构建使用多个数据库的工具(例如GUI数据库管理器)时才有用。

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.

这允许您在Tomcat中创建单个且可重复使用的连接池数据源。您可能只需要重写连接管理器和/或SQL查询。

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.

这篇关于如何管理动态创建的数据库的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆