How to check active transactions in SQL Server
Solved Email & Outlook
MS
Michael Scofield
January 29, 2021
2 replies
10,930 views
Reviewed by moderators

Application timeouts and blocking spikes suggest something is holding a transaction open on our production database. I need to see active transactions right now and identify the holder.

What are the commands and what should I read from their output before doing anything drastic?

Accepted Answer
Verified by David Taylor, Database Expert ยท Reviewed January 2021

Two tools, one quick and one detailed, then the judgment call the output feeds.

The quick one: DBCC OPENTRAN against the database names the oldest active transaction, its start time and its session id. One command, and in a blocking spike the oldest transaction is usually the story, an hour old start time on a session id gives you the suspect immediately.

The detailed view joins the transaction DMVs to the sessions: sys.dm_tran_active_transactions joined through sys.dm_tran_session_transactions to sys.dm_exec_sessions, adding sys.dm_exec_requests with a cross apply of sys.dm_exec_sql_text for whatever statement is currently running. The columns that matter: transaction_begin_time for age, session status where sleeping with an open transaction is the classic forgotten BEGIN TRAN, host_name, program_name and login_name for who, plus the sql text for what. A sleeping session from a workstation holding a two hour transaction reads very differently from an active bulk load, which is the whole point of looking before acting.

The judgment call: KILL session_id ends the holder, and its rollback can take as long as the work it undoes, sometimes much longer, during which the blocking continues. For the forgotten workstation BEGIN TRAN, kill freely, the rollback is trivial. For a long running legitimate process, waking its owner beats killing it mid flight. Check rollback progress afterwards with KILL session_id WITH STATUSONLY, and resist restarting the service to hurry a rollback, recovery replays the same undo at startup with the database unavailable throughout.

OPENTRAN pointed at a session sleeping for 94 minutes, the DMV join showed a developer workstation and an uncommitted UPDATE. One KILL, instant rollback, blocking evaporated. The developer has been gifted a SET IMPLICIT_TRANSACTIONS lecture.