This simple code produces deadlock. Simple Example Program included

后端 未结 3 923
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 17:52

Code, notice the order of the values is different. So it alternates between locking rows:

static void Main( string[] args )
        {
            List

        
3条回答
  •  爱一瞬间的悲伤
    2021-01-15 18:00

    Code, notice the order of the values is different. So it alternates between locking rows

    No, it doesn't alternate. It acquires the locks in two different order. Deadlock is guaranteed.

    Is it possible not to ... update just those 2 rows in parallel?

    Not like that it isn't. What you're asking for is the definition of a deadlock. Something gotta give. The solution must come from your business logic, there should be no attempts to process the same set of IDs from distinct transactions. What that means, is entire business specific. IF you cannot achieve that, then basically you are just begging for deadlocks. There are some things you can do, but none is bulletproof and all come at great cost. The problem is higher up the chain.

提交回复
热议问题