How to find leaking SQL Server connections


Summary

If your application is leaking connections to Microsoft SQLServer, you can find the last SQL statement that was run for a connection by running this query.


Detail

Run the following query:

	SELECT
			recent.text AS 'Last SQL Statement',
			connection.client_ip_address AS 'client IP',
			connection.connect_time AS 'connected since',
		FROM
			sys.dm_exec_connections AS connection
		CROSS APPLY
			sys.dm_exec_sql_text(connection.most_recent_sql_handle) AS recent
		ORDER BY
			connection.connect_time ASC
	

Note that I have only used it on SQLServer 2008!