/INBerkeley DB Reference Guide: Transaction Protected Applications[PH

Berkeley DB Reference Guide: Transaction Protected Applications



(

Deadlock Detection



EThe first component of the infrastructure, deadlock detection,>is not so much a requirement specific to transaction protectedGapplications, but rather is necessary for almost all applications whereFmore than a single thread of control will be accessing the database atBone time. (It is not required by all such applications, asKread-only applications cannot deadlock, however there are a very fewFapplications where the access pattern to the data is sufficiently wellEstructured that deadlock cannot occur.) Regardless, while Berkeley DB4automatically handles database locking issues, it is(normally possible for deadlock to occur.

EWhen the deadlock actually occurs, what happens is that two (or more)Cprocesses (or threads, i.e., any thread of control) that hold locksErequest other locks that cannot be granted, and will never be grantedHbecause one of the processes now waiting holds the resource in question.

IFor example, consider two processes A and B. Let's say that A obtains anKexclusive lock on item X, and B obtains an exclusive lock on item Y. Then,EA requests a lock on Y and B requests a lock on X. A will wait untilEresource Y becomes available and B will wait until resource X becomesGavailable. Unfortunately, since both A and B are waiting, neither willHrelease the locks they hold and neither will ever obtain the resource onFwhich it is waiting. In order to detect that deadlock has happened, aFseparate process or thread must review the locks currently held in theHdatabase. If deadlock has occurred, a victim must be selected, and thatFvictim will then return the error EAGAIN from the database call it wasmaking.

OBerkeley DB provides a separate UNIX-style utility which can be used to performjthis deadlock detection, named, cleverly enough, db_deadlock.DAlternatively, applications can create their own deadlock utility orqthread using the underlying lock_detect function, or specify thatMBerkeley DB run the deadlock detector internally whenever there is a conflict1over a lock (see lk_detect for more information).

@Deciding how often to run the deadlock detector and which of theDdeadlocked transactions will be forced to abort when the deadlock isCdetected is a common tuning parameter for Berkeley DB applications.

MARÿÿ