Unexpected, long running blocking transaction
I am messing around with a test database for the dev version of my we开发者_StackOverflow中文版bsite. Somehow, I have created a blocking transaction that I don't know how to get rid of.
I tried to run the following query:
SELECT COUNT(*) FROM Users
But the query doesn't go anywhere. I have found that the following transaction has blocked it:
TransactionID: 19593
Transaction name: user_transaction
Total Locks: 158,936
Transaction Type: Full Transaction
Isolation Level: Unknown
This transaction has been running for 90 minutes.
I belive it is related to this query I ran:
DECLARE @cursor CURSOR
DECLARE @userid uniqueidentifier
SET @cursor = CURSOR
FOR
SELECT TOP(1000) userid FROM Users
OPEN @cursor
FETCH NEXT FROM @cursor INTO @userid
DECLARE @counter int = 0
WHILE @@FETCH_STATUS = 0
BEGIN
SET @counter = @counter + 1
PRINT @Counter
EXEC sp_removeUser @userid, 0
FETCH NEXT FROM @cursor INTO @userid
END
(I know cursors are bad, but normally there would be no need to remove 5000 users at once.)
However, this query completed successfully, so I don't know why it might still have a transaction hanging around. I canceled a previous version of this query before it completed, would that have created a problem?
What could have caused this blocking transaction, and how do I terminate it?
To determine the cause of the blocking take a look at Understanding and resolving SQL Server blocking problems.
Though in your case you have already identified it and just need to execute COMMIT
in the SSMS window sufficient times to bring @@TRANCOUNT
to zero
WHILE @@TRANCOUNT > 0
COMMIT
精彩评论