Docsity

Locking Granularity and Two-Phase Locking Protocol in Database Systems, Lecture notes of Principles of Database Management

Partial preview of the text

Download Locking Granularity and Two-Phase Locking Protocol in Database Systems and more Lecture notes Principles of Database Management in PDF only on Docsity!

Locking Granularity

Locking Granularity

  • (^) Locking granularity is one complication about locks.
  • (^) Granularity refers to the size of the database item locked.
  • (^) Locking granularity is a trade-off between waiting time (amount of concurrency permitted) and overhead (number of locks held).
  • (^) Most DBMSs can hold locks for different granularities as shown below:
  • (^) The entire database is the coarsest lock that can be held.
  • (^) If an exclusive lock is held on the entire database, no other users can access the database until the lock is released.
  • (^) On the other extreme, an individual column is the finest lock that can be held.
  • (^) Locks also can be held on parts of the database not generally seen by users. For example, locks can be held on indexes and pages (physical records).
  • (^) Holding locks at a fine level decreases waiting among users but increases system overhead because more locks must be obtained.
  • (^) Holding locks at a coarser level reduces the number of locks but increases the amount of waiting.
  • (^) In some DBMSs, the concurrency control manager tries to detect the pattern of usage and promotes locks if needed.
  • (^) For example, the concurrency control manager initially can grant record locks to a transaction in anticipation that only a few records will be locked.
  • (^) If the transaction continues to request locks, the concurrency control component can promote the row locks to a lock on a subset of rows or the entire table.
  1. Intention shared when intending to read lower- level items
  2. Intention exclusive when intending to write lower- level items
  3. Shared intention exclusive when intending to read and write lower-level items.
  • (^) For example, a transaction should request an intention shared lock on a table for which it intends to read rows of the table. The intention shared lock on the table only conflicts with exclusive locks on the table or smaller grained items.

Deadlocks

  • (^) This is a problem of mutual waiting that can occur when using locks.
  • (^) One transaction has a resource that a second transaction needs, and' the second transaction holds a resource that the first transaction needs.
  • (^) If a deadlock is not resolved, the involved transactions will wait forever.
  • (^) A DBMS can control deadlocks through detection or a timeout policy.
  • (^) For example:
  • (^) Transaction A tries to lock the second leg but is blocked because transaction B holds an exclusive lock.
  • (^) Likewise, transaction B must wait to obtain a lock on the first leg.
  • (^) Deadlocks can involve more than two transactions, but the pattern is more complex.
  • (^) To control deadlocks, most enterprise DBMSs perform deadlock detection.
  • (^) Deadlocks can be detected by looking for cyclic patterns of mutual waiting.
  • (^) In practice, most deadlocks involve two or three transactions.
  • (^) Because deadlock detection can involve significant computation time, deadlock detection is only performed at periodic intervals or triggered by waiting transactions.
  • (^) For example, deadlock detection for the above example could be performed when transaction B is forced to wait.
  • (^) When a deadlock is detected, the most recent transaction (transaction B in above example) is usually forced to restart.
  • (^) Most desktop DBMSs use a simpler time-out policy to control deadlocks.

Two-Phase Locking Protocol

  • (^) To ensure that lost update problems do not occur, the concurrency control manager requires that all transactions follow the Two-Phase Locking (2PL protocol).
  • (^) Protocol is a fancy word for a group behavior rule.
  • (^) A protocol binds all members of a group to behave in a specified manner.
  • (^) For human communication, Robert's Rules of Order require that all meeting participants follow certain rules.
  • (^) For data communication, protocols ensure that messages have a common format that both sender and receiver can recognize.
  • (^) For concurrency control , all transactions must follow the 2PL protocol to ensure that concurrency control problems do not occur.
  • (^) Two-phase locking has three conditions:
  1. Before reading or writing to a data item, the transaction must acquire the applicable lock to the data item.
  2. Wait if a conflicting lock is held on the data item.
  3. After releasing a lock, the transaction does not acquire any new locks.
  • (^) The above diagram depicts the 2PL protocol with the simplified third condition.
  • (^) At the beginning of the transaction(BEGIN), a transaction has no locks.
  • (^) A growing phase ensues in which the transaction acquires locks but never releases any locks.
  • (^) At the end of the transaction (Lock Point), the shrinking phase occurs in which all locks are released together.
  • (^) Simplifying the definition of 2PL makes the protocol easier to enforce and obviates the difficult problem of predicting when a transaction may release locks.
  • (^) If conflicts are rare, it is more efficient to check for conflicts rather than use locks to force waiting.
  • (^) In optimistic approaches, transactions are permitted to access the database without acquiring locks.
  • (^) Instead, the concurrency control manager checks whether a conflict has occurred.
  • (^) The check can be performed either just before a transaction commits or after each read and write.
  • (^) By reviewing the relative time of reads and writes, the concurrency control manager can determine whether a conflict has occurred.
  • (^) If a conflict occurs, the concurrency control manager issues a rollback and restarts the offending transaction
  • (^) Despite the appeal of optimistic approaches, most organizations use 2PL even though some enterprise DBMSs support optimistic approaches.
  • (^) The performance of optimistic approaches depends on the frequency of conflicts.
  • (^) If conflicts increase, the performance of optimistic approaches decreases.
  • (^) Even if conflicts are rare, optimistic approaches can have more variability because the penalty for conflicts is larger in optimistic approaches.
  • (^) Pessimistic approaches resolve conflicts by waiting.