如何限制从我的 tomcat 部署的 java 应用程序到 SQL Server 服务器的连接数量?
我有一个应用程序部署在服务器 A 上的 tomcat 上,并将查询发送到服务器 B 上的各种 SQL Server 数据库。
我担心我的应用程序可能会过载此 SQL Server 数据库服务器,并且希望有某种方法来防止它发生如果任意数量的连接已存在且未关闭,则请求连接到该服务器上的任何数据库。
我正在考虑使用连接池,但我的印象是,这只会池化到 SQL Server 服务器上特定数据库的连接,我想控制许多不同数据库上发生的这些组合连接的总数(顺便说一句,我只能动态地找出各个数据库的名称,因为它们每天都在变化)。连接池会帮我解决这个问题吗?我是否从错误的角度看待这个问题?
我无权访问 SQL Server 服务器的配置。
非常欢迎指向您建议的解决方案的教程或工作示例的链接!
I have an application that is deployed on tomcat on server A and sends queries to a huge variety of SQL Server databases on an server B.
I am concerned that my application could overload this SQL Server database server and would like some way to preventing it making requests to connect to any database on that server if some arbitrary number of connections were already in existence and unclosed.
I am looking at using connection pooling but am under the impression that this will only pool connections to a specific database on the SQL Server server, I want to control the total of these combined connections that will occur to many different databases (incidentally I can only find out the names of individual db's dynamically as they change day to day). Will connection pooling take care of this for me, are am I looking at this from the wrong perspective?
I have no access to the configuration of the SQL Server server.
Links to tutorials or working examples of your suggested solution are most welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您考虑过使用 DBCP 吗?请参阅此处了解更多信息
Have you considered using DBCP? See here for more info
你是对的。数据库池将限制与数据库或所有数据库的连接,具体取决于池的配置方式。
这里有几个实现数据库连接池的开源包。其中之一可能会做你想要的事情。
You're correct. A database pool will limit connections to either the database, or to all databases, depending on how the pool is configured.
Here are several open source packages that implement database connection pools. One of them probably does what you want.
AFAIK,这是正确的。您的连接池将仅限制与其定义的特定数据库的连接数量。
我认为您无法控制池中到所有数据库的连接数量。您已经写道您无权更改 MSSQL 服务器上的内容。因此,在 MSSQL 本身上为各种数据库创建同义词并不是一种选择。
您可以在应用程序中编写自己的类,例如 ConnPoolManager,它在从任何池获取和释放连接之前有一个内部计数器。
此类应缓存对每个池的所有 JNDI 查找。
对于要获取到任何池的连接的应用程序,它会通过 ConnPoolManager,如果计数器显示尚未超过最大限制,则它才会获取连接。
否则它会抛出一些异常供您稍后尝试。
可能有一个类似于业务代表的设计模式。
话虽如此,我认为对您来说更大的问题是
,您每天都需要在 Tomcat 中创建新的或编辑连接池设置吗?这对未来的维护来说是一场噩梦。
AFAIK, this is correct. Your connection pool will only limit the number of connections to the specific database it is defined for.
I dont think you can control the number of connections to all databases from the pool. You've written you don't have access to change things on the MSSQL server. so creating SYNONYMs to the various databases on MSSQL itself is not an option.
You can write your own class within the application, say a ConnPoolManager, which has an internal counter prior to getting and releasing Connections from any of the pools.
This class should cache all the JNDI lookups to each pool.
For the app to get a connection to ANY pool, it goes through the ConnPoolManager and if the counter shows the maxlimit is not yet crossed, only then does it fetch the connection.
Else it throws some exception for you to try later.
There might be a design pattern for this on the lines of Business Delegate.
Having said that, I think a bigger problem for you will be
since you will be expected to create new or edit the connection pool settings in Tomcat each day? This is a maintenance nightmare in the future.