Introduction to Database Systems - transactions and concurrency control

I. Overview of the transaction

1.1 Definitions

Transaction is a sequence of user-defined database operations, these operations do either full or not full, is an indivisible unit of work
transaction is a basic unit of concurrency control, recovery of the database is the basic unit, the basic logical unit is a logical database
database recovery mechanism and concurrency control mechanism is an important part of the database management system
in a relational database, a transaction can be a SQL statement, a set of SQL statements or whole programs

--显式定义事务
begin transaction 
SQL 语句1 
……  ……
commit / rollback

/*
commit
    事务正常结束
    提交事务的所有操作(读+更新)
    事务中所有对数据库的更新写回到磁盘上的物理数据库中

rollback
    事务异常终止
    事务运行的过程中发生了故障,不能继续执行
    系统将事务中对数据库的所有已完成的操作全部撤销
    事务滚回到开始时的状态
*/
--将数学课程号由2修改为22
/*
解析:
所涉及的关系:Course和SC,根据参照完整性约束,应将两表中的2都改为22。要将修改两个表的两个SQL语句定义成一个事务。因为DBMS对这两个语句要么都执行,要不都不执行
*/

begin transaction
    update Course set CNO='22' where CNO='2'
    update SC set CNO='2' where CNO='22'
commit

1.2 Features

Atomicity

  • Logical unit of work is a transaction database
  • All operations in a transaction are either all successfully executed or not executed
  • If the transaction is aborted due to failure, will have to try to eliminate the effect produced by the firm, the database is restored to the state before the transaction execution

consistency

  • The results of the implementation of the database transaction must transition from one consistent state to another consistent state

Isolation

  • Execution of a transaction can not be other transactions interference. Concurrent execution of multiple transactions, update anything until it is successfully submitted, they are not visible to other matters

Endurance

  • After a transaction is complete, it changes to the database must be permanent, even if the system fails, it updates the database will be permanently effective

Second, Concurrency Control Overview

Concurrent execution of transactions is an effective way to improve system efficiency of database systems, but may damage the transaction ACID properties, resulting in data inconsistency

2.1 concurrent execution of transactions

1, the reasons for concurrent execution

  • Increase throughput and resource utilization
  • Reduce waiting time

2, concurrent implementation

  • Cross-concurrent mode
  1. Stand-alone system, the parallel operations in turn cross-running transaction
  2. Advantages: processor idle time can be reduced to improve the efficiency of the system
  3. Cons: not a true concurrent operation, may result in data inconsistency
  • Concurrent mode
  1. Multiprocessor system, each processor may run a transaction, multiple transactions with multiple processors may simultaneously achieve a plurality of transaction truly parallel operation
  2. Advantages: the best approach to concurrency
  3. Disadvantages: subject to the hardware environment, it may also generate data inconsistencies

2.2 concurrent execution problems caused

1, loss of modification
2, read "dirty" data
3, non-repeatable read
causes

  • After a data read transaction T1, T2 thereof has been modified transaction, transaction T1 reads the data again, and found different from the previous
  • After Transaction T1 reads certain data recorded under certain conditions, transaction T2 removed some of these records, transaction T1 is read again recorded under the same conditions, there is no record found that some
  • After Transaction T1 reads certain data recorded under certain conditions, some records are inserted transaction T2, T1 transaction record is read again under the same conditions, it was found a few more records

miD6sI.png

Example: a sequence of activities in the airline reservation system
① A ticket (transaction T1) read out a balance of flight tickets A, provided 16 = A;
② ticket acetate (transaction T2) read out the balance on the same flight tickets A, is also 16;
③ A ticket selling a ticket, adjusts the balance A ← A-1, so A 15, the A written to the database;
④ b a ticket also sell tickets, modify balance A ← A- 1, the a 15, a write back to the database
results obviously sell two tickets, ticket database only reduce the balance of 1
this condition is called inconsistency of the database, is caused by concurrent operation
under the concurrent operation case, scheduling operation sequences T1, T2 of the two transactions is random. If the scheduling performed in the above sequence, modification of the transaction T1 will be lost. Reason: covering the transaction T1 T2 after step 4. A transaction modified and written back modifications

Generating three inconsistency is the main reason for the above-described concurrent operation of destroying the isolation of the transaction. Concurrency control is the right way to use the scheduling of concurrent operation, the execution of a transaction without interference from other transactions
concurrency control of major technologies: blockade, timestamp, optimistic Control Act, multi-version concurrency control

Third, the blockade

Block That transaction prior to a data object manipulation, Xianxiang lock system sends a request to the transactional data objects have some control over the lock, before the transaction releases its lock, other transactions can not update the data object

3.1 The basic types of locks

Exclusive lock , also known as a write lock or X lock, if the transaction data object T to A plus X lock, only allows T to read and modify A, no other transaction can no longer read and modify the A, until the release of A T lock on
shared locks , also known as S locks or read locks, if the transaction data object a plus T S lock, then the transaction T can be read but can not modify a a, a on the other had only read transactions, and can not be modified, until T a releases the lock on the locking protocol 2.2

3.2 locking protocol

3.2.1 a locking protocol

Transaction T must increase its X lock before modifying the data object R, the transaction is not released until the end of the
action: a modified locking protocol to prevent the loss, and to ensure that the transaction is recoverable T
Description: In a blockade of the agreement, if is read the data is not to modify, lock is not required, so it can not be guaranteed and non-repeatable read read "dirty" data

3.2.2 two locking protocol

On the basis of a locking protocol on transaction T must be added its S lock before reading the data object R, S lock can be released but after reading
action: two locking protocol modifications and prevent loss of reading "dirty" data
Description: in two locking protocol, since the S lock can be released after the reading data, it does not guarantee a repeatable read

3.2.3 three locking protocol

On the basis of a locking protocol on transaction T must be added its S lock before reading data R, is not released until the end of transaction
functions: prevent the loss of three locking protocol modified to read the dirty data and non-repeatable read
three the main difference between the agreement

  • What actions need to apply to block and when to release the lock (ie holders of lock time)
  • The higher the locking protocol level, the higher the degree of consistency

miDvYF.png

Deadlock and livelock 3.3

3.3.1 Livelock

Also known as hunger, is a transaction waiting for lock is in a state of waiting indefinitely
avoid Livelock: first come, first serve, when more request to block the same data objects, according to the order the request queue blockade on these matters, the data lock on an object Once released, the first application for the approval of the first transaction in the queue to obtain a lock

3.3.2 Deadlock

Cycle between two or more transactions or wait for the phenomenon of
deadlock prevention
1, a one-time French blockade
after each transaction once all must lock all data objects to be used in the actual implementation of practical operation, otherwise the transaction is not nor were any real action to block any data
problems

  • The locking time data growth, reduce concurrency of the system
  • Determine in advance the data object is difficult to block the transaction

2, the order of the blockade law
pre-defined data objects a blockade order, all transactions in that order blockade
Problems

  • High maintenance costs, very much database system blocked data objects, and with the data insertions, deletions and other operations and is constantly changing, to maintain such resources is very difficult to blockade order

Although both methods are effective in preventing deadlock, but there are some problems, so it is not really easy to implement. Therefore, strategies to prevent deadlock is not very suitable for the characteristics of the database, DBMS commonly used method to diagnose deadlock and lifted

3.3.3 Deadlock Detection and released

1, deadlock detection
database systems typically employ time-out or waiting transaction method found deadlock FIG
2, release the occupied
select one or more transactions in the deadlocked, revoke and release all locks held by the transaction, thus breaking the cycle of waiting for conditions to lift the deadlock, so that other transactions can continue to run

Fourth, the scheduling concurrent serializable

4.1 serial schedule with concurrent scheduling

4.1.1 serial schedule

Serial schedule a set of transactions means that these transactions executed one after the other, in which each transaction on a transaction (if any) only started after a complete end
to a set of transactions, serial schedule is always correct
for n transaction, there are n! different serial schedule may lead to different results

4.1.2 Concurrent scheduling

A set of concurrent scheduling of things means that these transactions have at least two transactions have started their execution, and are not over
the correctness of the rule of concurrent scheduling: a set of concurrent scheduling of things is correct, if and only if the scheduled execution results results of the one of the same serial schedule, at this time said concurrent scheduling is serializable

4.2 conflict serializable

Conflicting operations: refers to the different transaction read and write operations and write operations on the same data

Ri(x)与Wj(x)         /*事务Ti读x,Tj写x,其中i≠j*/
Wi(x)与Wj(x)         /*事务Ti写x,Tj写x,其中i≠j*/

Action can not be exchanged includes two operations of the same transaction and conflicting operations of different transactions
scheduled Sc order conflicts in ensuring the same operation, by switching the order of two transaction does not conflict with another operation schedule obtained Sc ', if Sc 'is serial, Sc is called scheduling conflict serializable schedule. If a scheduling conflict serializable, it must be serializable scheduling
conflict serializable schedule is a sufficient condition serializable scheduling, is not a requirement

Five, two-phase locking protocol

"Two-stage" lock the meaning of the transaction in two stages
The first stage is to get the blockade, also known as expansion stage

  • Transactions can apply for any type of lock on any data item, but can not release any locks

The second stage is the release of blockade, also known as contraction phase

  • Any type of transaction may release locks on any of the items, but can not apply for any lock

The correctness of the two-phase locking protocol

  • If all transactions obey the two locking protocol, any concurrent scheduling policy on these matters are serializable
  • Two-phase locking concurrency control protocol is a sufficient condition for the correctness, but not a requirement. That is, if all transactions obey the two-phase locking protocol, any concurrent scheduling of these transactions are serializable, conversely, a concurrent schedule is serialized, not necessarily all transactions obey the two-phase locking protocol

Two-phase locking protocol and a blockade to prevent deadlock method

  • A blockade law requires that each transaction must once all the data you want to use all of the lock, otherwise it can not continue, so a two-phase locking protocol blockade law compliance
  • But the two-phase locking protocol does not require transaction must once all the data you want to use all locked, and therefore comply with two locking protocol transaction deadlocks may occur

Six multi-block size

6.1 size

Block size of the object is called the particle size, may be a database, tables, records, fields, and other
concurrency control overhead and concurrent blockade particle size is closely related to the system. Blocked larger particle size, the less the database can be a data block unit, the smaller the degree of concurrency, the smaller overhead; blocked smaller particle size, the higher the degree of concurrency, but the greater the system overhead
multiple granularity locking simultaneously support multiple lock-out procedures to block size selection for different transactions

  • Dependent data structures - Multiscale Tree

    The data objects are organized into a database by the relationship between particle size and the tree structure, the root node which represents the maximum data size, the data typically represents the minimum granularity for the entire database, leaf nodes

Multi-granularity locking protocol allows independent lock for each node in the multi-size tree, and locked to each node implies its descendants nodes be similarly locks

  • A data object may be blocking in two ways in a multi-granularity locking in: explicit and implicit Block Block
  1. Directly applied by the transaction data object called explicit blockade blockade
  2. By locking the upper node is blocked due subordinate object, called implicit Block
  3. Block explicit and implicit blockade effect is the same

To check the system conflict checking block explicit check block also implicit Block
multiple granularity locking method
for the data of a object locking system to check

  • The presence or absence of an object explicitly conflict with blockade
  • The upper node of the presence or absence of an explicit target block conflict with
  • Subordinate node of the presence or absence of an explicit target block conflict with

When no conflict can only lock success

6.2 intent locks

When he was on a node locked, you must first add its parent node intent lock
Objective: To improve the efficiency of the inspection system of a data object locking
action: to reduce the workload of the collision check blockade lock. Just check whether the parent node of this node has been added incompatible locks, and intent locks by this node to find out if there are subordinate node incompatible locks, eliminating the need to re-examine subordinate node
Three Intentions lock

  • Intent share lock (lock IS): if the rate of IS lock on a data object, indicating that it intends to descendant nodes (intention) was added S-lock
  • Lock IX: IX lock if added to a data object, indicating that it intends to descendants of the node (intention) plus X lock
  • SIX lock: if the increase of a data object SIX lock, showing its lock plus S, plus IX lock, i.e. SIX = S + IX

The strength of the lock: it refers to the degree of exclusion of other locks, a transaction at the time of application to the blockade instead of weak and strong lock lock safe, not vice versa
intent lock the blockade and the release order

  • We should be in the order from top to bottom when applying blockade
  • The order should be bottom-up release blockade

Guess you like

Origin www.cnblogs.com/xxwang1018/p/11546738.html