41

I have this open transaction, according to DBCC OPENTRAN:

Oldest active transaction:
SPID (server process ID) : 54
UID (user ID)            : -1
Name                     : UPDATE
LSN                      : (4196:12146:1)
Start time               : Jul 20 2011 12:44:23:590PM
SID                      : 0x01

Is there a way to kill it/ roll it back?

3 Answers 3

82

You should first figure out what it was doing, where it came from, and if applicable how much longer it might be expected to run:

SELECT 
   r.[session_id],
   c.[client_net_address],
   s.[host_name],
   c.[connect_time],
   [request_start_time] = s.[last_request_start_time],
   [current_time] = CURRENT_TIMESTAMP,
   r.[percent_complete],
   [estimated_finish_time] = DATEADD
       (
           MILLISECOND,
           r.[estimated_completion_time], 
           CURRENT_TIMESTAMP
       ),
   current_command = SUBSTRING
       (
           t.[text],
           r.[statement_start_offset]/2,
           COALESCE(NULLIF(r.[statement_end_offset], -1)/2, 2147483647)
       ),
   module = COALESCE(QUOTENAME(OBJECT_SCHEMA_NAME(t.[objectid], t.[dbid])) 
       + '.' + QUOTENAME(OBJECT_NAME(t.[objectid], t.[dbid])), '<ad hoc>'),
   [status] = UPPER(s.[status])
 FROM
     sys.dm_exec_connections AS c
 INNER JOIN
     sys.dm_exec_sessions AS s
     ON c.session_id = s.session_id
 LEFT OUTER JOIN
     sys.dm_exec_requests AS r
     ON r.[session_id] = s.[session_id]
 OUTER APPLY
     sys.dm_exec_sql_text(r.[sql_handle]) AS t
 WHERE
     c.session_id = 54;

If you are confident that you can sever this connection you can use:

KILL 54;

Just be aware that depending on what the session was doing it could leave data and/or the app that called it in a weird state.

2
  • 54
    Another easy way to see all open transactions on a server is pressing CTRL+1 in query window. It shows all current transactions. Commented Feb 2, 2015 at 19:18
  • 13
    @SimonDugré This short cut can be changed in the options, if someone changed this, it won't work as expected. Instead we can run the stored procedure sp_who which actually gets called when you press ctrl+1. Thanks Commented Apr 29, 2016 at 11:37
4

In cases of deadlock, the following query should be run at regular intervals.

DBCC opentran()

If then the same SPID number is returned multiple times in the following report

    Oldest active transaction:
    SPID (server process ID): 131
    UID (user ID) : -1
    Name          : implicit_transaction
    LSN           : (634998:226913:1)
    Start time    : Jan 19 2022  6:36:36:360PM
    SID           : 0x010500000000000515000000c6bb507a9dbeda5275b975547b3e0000
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

Completion time: 2022-01-19T18:36:38.8421769+03:00

Then make a detail query for this transaction. It is critical to permanently resolve the source of this problem.

exec sp_who2 131
exec sp_lock 131

After investigating the cause, you can resolve the deadlock by killing that process.

KILL 131

If you want to see all SPIDs and blocked as tables, you should use the following query.

SELECT spid, blocked,[dbid],last_batch,open_tran
FROM master.sys.sysprocesses
WHERE open_tran <> 0
1

I ended up running into the situation of locking up a sessions as reported by DBCC OPENTRAN but due to the corporate lock down of the Server/database my ability to KILL was not available.

I discovered that the app I was using to execute the script(s), VS 2022, was complicit, so to speak, in keeping the transactions alive. By closing the app, it notified me that there were active sessions running and that closing could have consequences. By accepting the notifications and closing the app, the open transactions would subsequently be closed.

1
  • "keeping the transactions alive. By closing the app" this solved my issue. (You saved the rest of my evening, thank you)
    – MaanooAk
    Commented Dec 22, 2023 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.