Why Is My Azure SQL Database Table Permanently Locked?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 18:30:55

问题


I have an isolated Azure SQL test database that has no active connections except my development machine through SSMS and a development web application instance. I am the only one using this database.

I am running some tests on a table of ~1M records where we need to do a large UPDATE to data in nearly all of the ~1M records.

DECLARE @BatchSize INT = 1000

WHILE @BatchSize > 0
BEGIN
    UPDATE TOP (@BatchSize)
        [MyTable]
    SET
        [Data] = [Data] + ' a change'
    WHERE
        [Data] IS NOT NULL

    SET @BatchSize = @@ROWCOUNT
    RAISERROR('Updated %d records', 0, 1, @BatchSize) WITH NOWAIT
END

This query works fine, and I can see my data being updated 1000 records at a time every few seconds.

Performing additional INSERT/UPDATE/DELETE commands on MyTable seem to be somewhat affected by this batch query running, but these operations do execute within a few seconds when ran. I assume this is because locks are being taken on MyTable and my other commands will execute in between the batch query's locks/looping iterations.

This behavior is all expected.

However, every so often while the batch query is running I notice that additional INSERT/UPDATE/DELETE commands on MyTable will no longer execute. They always time out/never finish. I assume some type of lock has occurred on MyTable, but it seems that the lock is never being released. Further, even if I cancel the long-running update batch query I can still no longer run any INSERT/UPDATE/DELETE commands on MyTable. Even after 10-15 minutes of the database sitting stale with nothing happening on it anymore I cannot execute write commands on MyTable. The only way I have found to "free up" the database from whatever is occurring is to scale it up and down to a new pricing tier. I assume that this pricing tier change is recycling/rebooting the instance or something.

I have reproduced this behavior multiple times during my testing today.

What is going on here?


回答1:


Scaling up/down the tier rollback all open transactions and disconnect server logins.

About what you are seeing it seems is lock escalation. Try to serialize access to the database using sp_getapplock. You can also try lock hints.



来源:https://stackoverflow.com/questions/49057593/why-is-my-azure-sql-database-table-permanently-locked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!