What are common reasons for deadlocks?

后端 未结 12 994
清酒与你
清酒与你 2020-12-13 00:16

Deadlocks are hard to find and very uncomfortable to remove.

How can I find error sources for deadlocks in my code? Are there any \"deadlock patterns\"?

In m

相关标签:
12条回答
  • 2020-12-13 00:59

    Making sure all transactions affect tables in the same order is the key to avoiding the most common of deadlocks.

    For example:

    Transaction A

    UPDATE Table A SET Foo = 'Bar'
    UPDATE Table B SET Bar = 'Foo'
    

    Transaction B

    UPDATE Table B SET Bar = 'Foo'
    UPDATE Table A SET Foo = 'Bar'
    

    This is extremely likely to result in a deadlock as Transaction A gets a lock on Table A, Transaction B gets a lock on table B, therefore neither of them get a lock for their second command until the other has finished.

    All other forms of deadlocks are generally caused through high intensity use and SQL Server deadlocking internally whilst allocated resources.

    0 讨论(0)
  • 2020-12-13 00:59

    Deadlock occurs mainly when there are multiple dependent locks exist. In a thread and another thread tries to lock the mutex in reverse order occurs. One should pay attention to use a mutex to avoid deadlocks.

    Be sure to complete the operation after releasing the lock. If you have multiple locks, such as access order is ABC, releasing order should also be ABC.

    0 讨论(0)
  • 2020-12-13 01:01

    In my last project I faced a problem with deadlocks in an sql Server Database. The problem in finding the reason was, that my software and a third party software are using the same Database and are working on the same tables. It was very hard to find out, what causes the deadlocks. I ended up writing an sql-query to find out which processes an which sql-Statements are causing the deadlocks. You can find that statement here: Deadlocks on SQL-Server

    0 讨论(0)
  • 2020-12-13 01:04

    I recommend reading this article by Herb Sutter. It explains the reasons behind deadlocking issues and puts forward a framework in this article to tackle this problem.

    0 讨论(0)
  • 2020-12-13 01:04

    To avoid the deadlock there is a algorithm called Banker's algorithm.

    This one also provides helpful information to avoid deadlock.

    0 讨论(0)
  • 2020-12-13 01:07

    A condition that occure whene two process are each waiting for the othere to complete befoure preceding.the result is both procedure is hang. its most comonelly multitasking and clint/server.

    0 讨论(0)
提交回复
热议问题