Skip to content

Sessions by status

User sessions grouped by status (running, sleeping, dormant), from sys.dm_exec_sessions. Many sleeping sessions usually mean an oversized connection pool.

SQL Server · SQL
SELECT status,       COUNT(*) AS sessionsFROM sys.dm_exec_sessionsWHERE is_user_process = 1GROUP BY statusORDER BY sessions DESC

This query is part of the "SQL Server health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What the query shows

This query counts the user sessions on the server, grouped by their status, using sys.dm_exec_sessions. The filter is_user_process = 1 leaves system sessions out. The result is a handful of rows, which makes it a good cheap health check.

Run it when the application reports connection errors, when you suspect a connection leak or a pool that is too large, or to get a feel for how busy the server is.

How to read each column

  • status: running means the session is executing one or more requests. sleeping means it is connected and has no request running. dormant means the session was reset by connection pooling and is waiting in a pre-login state. preconnect means it is in the Resource Governor classifier.
  • sessions: how many sessions have that status right now.

Healthy and worrying results

Most sessions sleeping is normal. An application that uses a connection pool keeps connections open and idle between requests. Running should be a small number, close to the number of queries really executing at that moment.

Worry about trends, not about one reading. As a rule of thumb, a sleeping count that climbs for hours and never comes down suggests a connection leak. A running count that stays above the number of CPU cores for long suggests the server is saturated. Compare with the maximum allowed.

SELECT @@MAX_CONNECTIONS AS max_connections;

What to do about it

To find who owns the sessions, group by login, host and program.

Who holds the sessions
SELECT login_name, host_name, program_name, status, COUNT(*) AS sessionsFROM sys.dm_exec_sessionsWHERE is_user_process = 1GROUP BY login_name, host_name, program_name, statusORDER BY sessions DESC;

A sleeping session that holds an open transaction is the dangerous kind, because its locks block others. This query finds them.

Sleeping sessions with an open transaction
SELECT s.session_id, s.login_name, s.program_name, s.last_request_end_timeFROM sys.dm_exec_sessions sWHERE s.is_user_process = 1  AND s.status = 'sleeping'  AND s.open_transaction_count > 0;

If one application owns thousands of sleeping sessions, lower the maximum pool size in its connection string and make sure the code closes connections. Tuning the pool is almost always better than raising the server limit.

Limits to know

  • It counts sessions, not requests. One running session may be one query or a batch of several statements.
  • It is a snapshot. A burst of connections that opens and closes between runs does not show.
  • Seeing sessions from other users needs VIEW SERVER STATE (VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later). Without it you see only your own session.
  • On Azure SQL Database it needs VIEW DATABASE STATE and shows only the connections to that database.
  • The status value is the one reported by the server. Do not compare it with the text of a request status in sys.dm_exec_requests, which has other values.

Run it on a schedule in DataRunner

Schedule this query every hour or so in DataRunner and send it by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets. The run history of the schedule gives you the series you need to tell a pool that is large and stable from a leak that grows.

Frequently asked questions

What is the difference between running and sleeping sessions in SQL Server?
A running session is executing at least one request right now. A sleeping session is connected but idle, with no request in progress. Most pooled applications have many sleeping sessions, and that is normal.
What is a dormant session?
It is a session that connection pooling has reset and that is waiting in a pre-login state, ready to be reused. Dormant sessions are a sign of connection pooling at work.
How many sessions can SQL Server handle?
The ceiling is @@MAX_CONNECTIONS, which defaults to 32,767, but the practical limit is memory and CPU. Compare the number of sessions over time against what the application needs, not against that ceiling.
How do I find sleeping sessions that hold locks?
Filter sys.dm_exec_sessions for status sleeping with open_transaction_count above 0. Those sessions opened a transaction and did not close it, and they are usual suspects when other sessions are blocked.