Practically any application that uses locking may deadlock. The only exception to this rule is when all the threads of control accessing the database are read-only or when the Berkeley DB Concurrent Data Store product is used; the Berkeley DB Concurrent Data Store product guarantees deadlock-free operation at the expense of reduced concurrency.
The db_deadlock utility performs deadlock detection by calling lock_detect at regular intervals; the lock_detect function runs the Berkeley DB deadlock detector. When a deadlock exists in the system, all the threads of control involved in the deadlock are, by definition, waiting on a lock. The deadlock detector examines the state of the lock manager and identifies a deadlock, and selects one of the participants to abort. (See Configuring locking for a discussion of how a participant is selected). The lock_get or lock_vec call for which the selected participant is waiting then returns a DB_LOCK_DEADLOCK error. When using the Berkeley DB access methods, this error return is propagated back through the Berkeley DB interface to the calling application.
The deadlock detector identifies deadlocks by looking for a cycle in what is commonly referred to as its "waits-for" graph. More precisely, the deadlock detector reads through the lock table, and reviews each lock object currently locked. Each object has lockers that currently hold locks on the object and possibly a list of lockers waiting for a lock on the object. Each object's list of waiting lockers defines a partial ordering. That is, for a particular object, every waiting locker comes after every holding locker because that holding locker must release its lock before the waiting locker can make forward progress. Conceptually, after each object has been examined, the partial orderings are topologically sorted. If this topological sort reveals any cycles, the lockers forming the cycle are involved in a deadlock. One of the lockers is selected for abortion.
It is possible that aborting a single transaction involved in a deadlock is not enough to allow other transactions to make forward progress. Unfortunately, at the time a transaction is selected for abortion, there is not enough information available to determine whether aborting that single transaction will allow forward progress or not. Because most applications have few deadlocks, Berkeley DB takes the conservative approach, aborting as few transactions as may be necessary to resolve the existing deadlocks. In particular, for each unique cycle found in the waits-for graph described in the previous paragraph, only one transaction is selected for abortion. However, if there are multiple cycles, one transaction from each cycle is selected for abortion. Only after the aborting transactions have received the deadlock return and aborted their transactions can it be determined whether it is necessary to abort additional transactions in order to allow forward progress.
Copyright Sleepycat Software