简体   繁体   English

是否可以限制.NET中数据库连接的总数?

[英]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.

相关问题 如何限制.Net Web应用程序的ODBC连接数? - How to limit number of ODBC connections from a .Net web application? IIS。 是否可以将网站配置为限制连接数 = 1,但是所有新连接都将放在队列中? - IIS. Is it possible to configure the website to have limit number of connections = 1 however all new connections would be placed in Queue? Winsock是否限制并发连接数? - Does Winsock limit the number of concurrent connections? 有助于减少与数据库的连接数 - help with reducing the number of connections to the database 使用实体框架C#限制与数据库的连接 - Limit Connections to Database with Entity Framework C# Datatable中的记录总数限制是多少? - What is the limit for total number of records in Datatable? 如何获取服务器上打开的HTTP连接的总数? - How to get total number of open HTTP connections on a server? 是否可以通过Npgsql获取与数据库的所有连接? - Is it possible to get all the connections made to the database by Npgsql? 大量打开的数据库连接使应用程序崩溃 - Large number of open connections to database crashes the application SmtpClient:最大并发连接数超过了限制 - SmtpClient: The maximum number of concurrent connections has exceeded a limit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM