entity-framework | sql-server

Determine Total Number of Open/Active Connections in MS SQL Server

This article contains a SQL query which can be used to determine the total number of open/active connections in MS SQL Server. Also explains, how to solve the error "Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached."

Abhith Rajan
Abhith RajanSeptember 16, 2017 · 2 min read · Last Updated:

This article contains a SQL query which can be used to determine the total number of open/active connections in MS SQL Server. Also explains, how to solve the error “Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.”

Recently we migrated one of our application from one VPS server to another. Initially, everything was fine. After a day or so here comes a strange error.

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.

The connection string doesn’t have any custom value set for Max Pool Size. So the maximum pool size for the application was the default, i.e 100. To check whether the DB have this much open connection, we run the following query in the SQL Management Studio,

SELECT
    DB_NAME(dbid) as DBName,
    COUNT(dbid) as NumberOfConnections,
    loginame as LoginName
FROM
    sys.sysprocesses
WHERE
    dbid > 0
GROUP BY
    dbid, loginame

This will list all the DB’s and its number of open connections grouped by login name. For us, the number of open connection for the DB was ~100 that’s why the error. So we updated the connection string to have Max Pool Size=200. And the application worked fine. Sample connection string with Max Pool Size given below,

<add connectionString="Server=YOUR_SERVER;Database=YOUR_DB_NAME;User Id=YOUR_DB_USER_ID;Password=YOUR_DB_USER_PASSWORD;Max Pool Size=200;" name="Default" providerName="System.Data.SqlClient" />

Few things to remember in this kind of situations are,

  • In your code, make sure all the connections are closed when finished usage.
  • Don’t set Max Pool Size to a very large value unless it is necessary.

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub


Abhith Rajan

Written byAbhith Rajan
Abhith Rajan is a software engineer by day and a full-stack developer by night. He's coding for almost a decade now. He codes 🧑‍💻, write ✍️, learn 📖 and advocate 👍.
Connect

Is this page helpful?

Related SnippetsView All

Related ArticlesView All

Related VideosView All

High Availability and SLA for Azure SQL Managed Instance

Using SQL Data Sync for Bidirectional Data Replication in SQL Server & Azure SQL DBs | Data Exposed

Build fast, scalable data system on Azure SQL Database Hyperscale | Clearent

Related Tools & ServicesView All

sqlfum.pt

sequel fumpt

sqlfmt is an online SQL formatter. It is pronounced sequel fumpt. Its purpose is to beautifully format SQL statements.
SmarterASP.NET

SmarterASP.net - Unlimited ASP.NET Web Hosting

ASP.NET Hosting by SmarterASP.net. Unlimited ASP.NET Hosting Plans Starting at $2.95 a month.