[英]Is it possible to limit the total number of database connections in .NET?
I have an API server that interacts with an Azure-hosted PostgreSQL database instance. 我有一个与Azure托管的PostgreSQL数据库实例进行交互的API服务器。 There are a large number (>50) of postgres databases on that one postgres server. 一台postgres服务器上有大量(> 50个)postgres数据库。 Any given API request may have to interact with any given database. 任何给定的API请求可能必须与任何给定的数据库进行交互。
Unfortunately, our Azure plan for Postgres only allows 50 connections. 不幸的是,我们的Postgres Azure计划仅允许50个连接。 I regularly have requests fail because Postgres won't accept more. 我经常收到失败的请求,因为Postgres不会接受更多请求。 My ADO.NET connection pool is still holding onto database connections for recently used databases, while connections to other databases error out. 我的ADO.NET连接池仍保持最近使用的数据库的数据库连接,而与其他数据库的连接出错。
I've tried setting the Max Pool Size
on my connection strings, but it appears that the connection pool limit is applied per database , not per server . 我尝试在连接字符串上设置“ Max Pool Size
”,但是似乎连接池限制是针对每个数据库而不是每个服务器应用的 。 I still need as much pooling as I can get, opening new connections can take >1500ms, which is beyond my SLA if it happens on every request. 我仍然需要尽可能多的缓冲池,打开新连接可能需要> 1500ms,如果每次请求都发生这种情况,这超出了我的SLA。
Is there a way to ask .NET to never open more than 50 database connections, either per server or total? 是否有一种方法可以要求.NET永远不要为每个服务器或总数打开50个以上的数据库连接?
Set Max Pool Size
and instead of connecting to a separate database connect to the same database on the server and then execute the \\connect
statement to change to the desired database. 设置Max Pool Size
,而不是连接到单独的数据库,而是连接到服务器上的同一数据库,然后执行\\connect
语句以更改为所需的数据库。 The following code fragment demonstrates creating an initial connection to the master
database and then switching to the desired database specified in the databaseName
string variable. 以下代码片段演示了如何建立与master
数据库的初始连接,然后切换到databaseName
字符串变量中指定的所需数据库。
// Assumes that command is a NpgsqlCommand object and that
// connectionString connects to master.
command.Text = "\connect DatabaseName";
using (NpgsqlConnection connection = new NpgsqlConnection(
connectionString))
{
connection.Open();
command.ExecuteNonQuery();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.