Problems explaining deadlock chain

  • Hi,

    We are currently in a spot of bother, attempting to explain a deadlock chain and would really really appreciate any guidance/help in explaining what exactly happening and possible ways to mitigate please.

    Here's a brief overview of the scenario.

    There is one SP 'sp_Select_ActionsForProcessing' that is invoked at the same time by multiple threads. The SP selects a range of qualified records and selects the top 1 qualifying record. When the second thread runs, it will select the qualifying range excluding the one selected by the first one and so forth. We implemented the same with UPDATELOCK HOLDLOCK table hint, However, we do suspect the same to be the cause of contention, but could not pinpoint.

    Will appreciate any help please.

    Attached is the .xdl file, .xml and proc text.

  • I think you shold take out the HOLDLOCK. HOLDLOCK is the same as SERIALIZABLE, that is protection against "phantom reads", that is rows that were inserted since you read the range. Since your procedure is not inserting any rows, I don't see you need it.

    The serializable isolation level is quite prone to deadlocks, so avoid it if possible.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Thanks a lot for your insight. We also believed in the same way, specifically, the victim's request on the resource was for an update lock.

    We were and still are a bit baffled with interpreting the deadlock chain. If you did manage to have a look at the .xdl file, in one chain, it is shown, one process has been granted exclusive lock, however, in the chain involving deadlock, another process has been granted Update lock on the same keyid.

    Thanks and regards.

  • I will have to admit that I did not look at the deadlock chain. My experience of serializable was enough.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Apology for being late in getting back. You were quite correct. Getting rid of the HOLDLOCK solve the issue, however, teh DEADLOCK took other SPs as preferred partner. 🙂

    I did suggest to try with READ_COMMITTED_SNAPSHOT isolation level as the default isolation behavior, but the technical architects are a bit jittery as it will make the tempdb to take the toll. We have implemented the TRY..CATCH method for troubleshooting. Please advise if this is a correct approach.

    Regards.

  • READ_COMMITTED_SNAPSHOT is an excellent idea, but it will not apply in this case, as UPDLOCK implies REPEATABLE READ.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • In these cases with complicated code that takes different kinds of locks and you have problems with deadlocks it might be a good idea to explicitly take an application lock at the beginning of each stored procedure that manipulates these tables.

    Like this:

    BEGIN TRAN

    exec sp_getapplock 'MyRersourceName', 'Exclusive'

    -- Application code

    ..

    COMMIT

    Of course this might lower the concurrency so it could lower the performance but at least you will not have to worry about deadlocks.

    The lock taken by sp_getapplock when called in this way is always held until the end of the transaction.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply