Chapter 11 Concurrency Control

Chapter 11 Concurrency Control | Collation of Database Knowledge Points

Chapter 11 Concurrency Control

learn

  • The Necessity of Database Concurrency Control Technology
  • The concept of livelock and deadlock

master

  • Concurrent operations may cause data inconsistency, including lost modification, non-repeatable read, and read "dirty" data, etc.
  • Types of blockade and the nature and definition of different block types (X lock, S lock), related compatibility control matrix
  • The concept of a block agreement
  • The concept of blocking granularity, multi-granularity blocking method, compatible control matrix of multi-granularity blocking protocol
  • The relationship between blocking protocols and data consistency, the concept of serializability of concurrent scheduling
  • The relationship between the two-stage lock protocol and serializability, the relationship between the two-stage lock protocol and deadlock

knowledge points

  • Why concurrency control in the database? What characteristics of transactions can be guaranteed by concurrency control technology?

    • The database is a shared resource, and there are usually many transactions running at the same time. Simultaneous reading and/or modification of the same data occurs when multiple transactions access the database concurrently. If the concurrent operation is not controlled, incorrect data may be accessed and stored, destroying the consistency of the database. Therefore, the database management system must provide a concurrency control mechanism.
    • Concurrency control can ensure the [consistency] and [isolation] of transactions

    What types of data inconsistencies may be generated by concurrent operations? In what way can various inconsistencies be avoided?

    • Data inconsistency caused by concurrent operations includes three types
      • Lost modification (LostUpdate)
        Two transactions T1 and T2 read the same data and modify it. The result submitted by T2 destroys (overwrites) the result submitted by T1, causing the modification of T1 to be lost.
      • Non-repeatable read (Non-RepeatableRead)
        Non-repeatable read means that after transaction T1 reads data, transaction T2 performs an update operation, so that T1 cannot reproduce the previous reading result. Non-repeatable reading includes three situations 1. After transaction T1 reads a certain data, transaction T2 modifies it. When transaction T1 reads and modifies the data again, it gets a different value from the previous one. 2. Transaction T1 changes from After reading some data records in the database, transaction T2 deletes some of them. When T1 reads the data again according to the same conditions, it finds that some records have disappeared. 3. Transaction T1 reads some records from the database according to one condition. After the data is recorded, transaction T2 inserts some records. When T1 reads the data again according to the same conditions, it finds that after some more records, the two kinds of non-repeatable reads are sometimes called phantom rows​​​​
      • Reading "dirty" data (DirtyRead)
        reading "dirty" data means that transaction T1 modifies a certain data and writes it back to disk. After transaction T2 reads the same data, T1 is revoked for some reason, and T1 has been modified at this time. If the old data is restored to its original value, the data read by T2 is inconsistent with the data in the database, and the data read by T2 is "dirty" data, that is, incorrect data.
    • The method and technique to avoid inconsistency is concurrency control. Commonly used concurrency control technologies include blocking technology, time stamp method, optimistic control method, multi-version concurrency control method, etc.

    What is a blockade? What are the basic block types? Discuss their meanings.

    • Blocking means that transaction T first sends a request to the system to lock it before operating on a data object such as a table or record. After locking, transaction T has certain control over the data object. Before transaction T releases its lock, other transactions cannot update this data object.
    • There are two basic types of blocking: exclusive locks (referred to as X locks) and shared locks (referred to as S locks).
    • Exclusive locks are also called write locks. If transaction T adds an X lock to data object A, only T is allowed to read and modify A, and no other transaction can add any type of lock to A until T releases the lock on A. This ensures that other transactions can no longer read and modify A until T releases the lock on A.
    • Shared locks are also called read locks. If transaction T adds S lock to data object A, then transaction T can read A but cannot modify A. Other transactions can only add S lock to A, but cannot add X lock until T releases the S lock on A. This ensures that other transactions can read A, but cannot make any changes to A until T releases the S lock on A.

    How to use the blocking mechanism to ensure data consistency

    • The DBMS first performs a blocking operation on the data before reading and writing the data

      img

    What is a livelock? The cause and solution of livelock

    • img
    • The cause of livelock: When a series of blockades cannot be executed in their order, it may cause some transactions to wait indefinitely for a blockade, resulting in livelock.
    • An easy way to avoid livelocks is to use a first-come, first-served policy. When multiple transactions request to block the same data object, the blocking subsystem queues up the transactions in the order in which they are requested to be blocked. Once the lock on the data object is released, the first transaction in the application queue is approved to obtain the lock.

    What is deadlock? Please give some methods to prevent deadlock.

    img

    • In the database, the reason for deadlock is that two or more transactions have blocked some data objects, and then both request the data that has been blocked by other transactions to be locked, resulting in dead waiting. Preventing the occurrence of deadlock is actually to destroy the conditions for deadlock. There are usually two ways to prevent deadlocks:
      • The one-time blocking method
        requires that each transaction must lock all the data to be used at one time, otherwise it cannot continue to execute.
      • Sequential blocking method
        Predefines a blocking sequence for data objects, and all transactions are blocked in this sequence.

    Please give a method to detect the occurrence of deadlock, and how to remove the deadlock when the deadlock occurs?

    • The database system generally adopts the method of allowing the deadlock to occur, and the DBMS detects the deadlock and releases it.
    • The method of diagnosing deadlock in DBMS is similar to that of the operating system, and generally uses the timeout method or the transaction wait diagram method.
    • Timeout method: If the waiting time of a transaction exceeds the specified time limit, it is considered that a deadlock has occurred.
    • After the DBMS concurrency control subsystem detects a deadlock, it must try to remove it. The usual method is to choose a transaction with the least deadlock cost, undo it, and release all locks held by this transaction, so that other transactions can continue to run.

    What kind of concurrent scheduling is the correct scheduling?

    • A serializable schedule is the correct schedule.
    • Definition of serializable scheduling: The concurrent execution of multiple transactions is correct if and only if the result is the same as when they are executed serially in a certain order, we call this scheduling strategy serializable customized scheduling.
  • Comprehensive question

    img

    img

    img

    img

  • img

  • Proof

    img

    img

  • img

  • img

  • img

    Why introduce intent locks? What is the meaning of intent lock?

    • Intent locks are introduced to improve the efficiency of the blocking subsystem.
    • The reason is: In the multi-granularity locking method, a data object may be locked in two ways—explicit and implicit. Therefore, when the system locks a data object, it not only needs to check whether the data object has a (explicit and implicit) lock that conflicts with it; it also needs to check all its upper-level nodes and all lower-level nodes. conflicts with blockades (explicit and implicit) on these nodes; obviously, such a check is inefficient. Intention locks are introduced for this purpose.
    • The meaning of the intention lock is: when locking any node, you must first add an intention lock to its upper node. After the introduction of intent locks, the system does not need to check for conflicts with the blocking of the next-level nodes one by one when locking a certain data object.
  • Describe the commonly used intention locks: IS lock, IX lock, SIX lock, and give the compatibility matrix of these locks.

    • IS lock: If an IS lock is added to a data object, it means that its descendant node intends (intention) to add an S lock. For example, to add an S lock to a tuple, you must first add an IS lock to the relationship and database

    • IX lock: If an IX lock is added to a data object, it means that its descendant nodes intend (intention) to add an X lock. For example, to add an X lock to a tuple, you must first add an IX lock to the relationship and the database.

    • SIX lock: If a SIX lock is added to a data object, it means that an S lock is added to it, and an IX lock is added, that is, SIX = S + IX.

    • compatibility matrix

      img

Replenish

    • Try to describe the concept of two-stage lock protocol.

      • The two-stage lock protocol means that all transactions must lock and unlock data items in two stages.
        • Before reading and writing any data, you must first apply for and obtain the blockade of the data;
        • After releasing a lock, the transaction does not apply for and acquire any other locks.
      • "Two phases" means that the transaction is divided into two phases:
        • The first phase is the gain block, also known as the expansion phase. At this stage, the transaction can apply for any type of lock on any data item, but cannot release any lock.
        • The second phase is the release of the blockade, also known as the contraction phase. At this stage, the transaction releases the locks it has acquired, but cannot apply for any more locks.
    • What is the relationship between different locking protocols and the consistency level of the system?

      • Different blocking protocols correspond to different consistency levels.
      • The first-level locking protocol prevents lost modifications and guarantees that transaction T is recoverable. In the first-level blocking protocol, no S lock is added to the read data, so it cannot guarantee repeatable reading and "dirty" data will not be read.
      • In addition to preventing lost modification, the second-level locking protocol can further prevent reading "dirty" data. In the second-level locking protocol, since the S lock is released immediately after reading the data, it cannot guarantee repeatable reading.
      • In the three-level blocking protocol, whether it is reading data or writing data, the lock is lengthened, that is, the lock is not released until the end of the transaction. Therefore, in addition to preventing loss of modification and non-reading of "dirty" data, the three-level blocking protocol further prevents non-repeatable reading.
    • What is a Blocking Protocol? What are the main differences between the different levels of blocking protocols?

      • When using locking technology to lock data, some rules should be agreed upon. For example, when using X locks and S locks to lock data objects, it is necessary to agree on when to apply for X locks or S locks, when to release the locks, and so on. These agreements or rules are called Locking Protocol. Different rules are agreed on the blocking method, and various blocking agreements are formed. Different levels of blocking protocols, such as the three-level blocking protocol introduced in "Introduction", the main difference between the three-level protocols is what operations need to apply for blocking, when to apply for blocking, and when to release the lock (that is, the length of time the lock is held).
      • First-level blocking protocol: before the transaction T modifies the data R, it must first add an X lock to it, and it will not be released until the end of the transaction.
      • Second-level blocking protocol: first-level blocking protocol plus transaction T must add S lock to data R before reading it, and release S lock after reading.
      • Three-level blocking protocol: one-level blocking protocol plus transaction T must add S lock to data R before reading it, and it will not be released until the end of the transaction.
    • Why there is a SIX lock in the intention lock, but no XIS lock

      img

    • Whether integrity constraints can ensure that the database is in a consistent state when processing multiple transactions

      img

    • Comprehensive question

      img

      img

    • To prevent one user's work from unduly affecting another user, [concurrency control] should be taken

    • [Blocking] technology is generally used to solve the data inconsistency problem caused by concurrent operations.

    • The following problems that are not caused by concurrent operations are [deadlock]

    • DBMS generally adopt [blocking] method to ensure the correctness of scheduling

    • Before the transaction T modifies the data R, it must first add an X lock to it, and it will not be released until the end of the transaction. This is [level one blocking protocol]

    • If transaction T acquires an exclusive lock on data item Q, then T is able to read and write to Q [both readable and writable]

    • Suppose transactions T1 and T2 operate on data A in the database, there may be the following situations, which one will not cause conflict operations [T1 is reading A, T2 also reads A]

    • If there are two transactions operating on the same data in the database at the same time, the operation that will not cause conflicts is [both are SELECT]

    • In a database system, a deadlock is a [transaction failure]

Guess you like

Origin blog.csdn.net/m0_53870075/article/details/124426782