Skip to content

Instantly share code, notes, and snippets.

@kedzior-io
Created October 26, 2022 18:27
Show Gist options
  • Save kedzior-io/5a7a3a85d888fa138eb1e0867624afc5 to your computer and use it in GitHub Desktop.
Save kedzior-io/5a7a3a85d888fa138eb1e0867624afc5 to your computer and use it in GitHub Desktop.
SQL Server query for SqlClient Sleepers (Timeout period elapsed obtaining a connection from the pool)
-- From : Michael J Swart (https://sqlperformance.com/2017/07/sql-performance/find-database-connection-leaks)
declare @database_name sysname = N'YOUR_DATABASE_NAME_HERE';
declare @program_name sysname = N'.Net SqlClient Data Provider';
select
datediff(SECOND, s.last_request_end_time, getdate()) as seconds_asleep,
s.last_request_start_time as lastreq,
s.session_id,
db_name(s.database_id) as database_name,
s.host_name,
s.host_process_id,
t.text as last_sql,
s.program_name
from sys.dm_exec_connections c
join sys.dm_exec_sessions s
on c.session_id = s.session_id
cross apply sys.dm_exec_sql_text(c.most_recent_sql_handle) t
where s.is_user_process = 1
and s.status = 'sleeping'
and db_name(s.database_id) = @database_name
and program_name = @program_name
and datediff(second, s.last_request_end_time, getdate()) > 60
order by s.last_request_end_time;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment