Database - transactions and concurrency control

Copyright: Reprinted indicate the original works of the address https://blog.csdn.net/zjuwxx/article/details/90346903

table of Contents

I. Overview of the transaction

1.1 Definitions

1.2 Features

Second, Concurrency Control Overview

2.1 concurrent execution of transactions

2.2 concurrent execution problems caused

2.2.1 Modify loss

2.2.2 to read "dirty" data

2.2.3 Non-repeatable read

Third, the blockade

3.1 The basic types of locks

3.2 locking protocol

3.2.1 a locking protocol

3.2.2 two locking protocol

3.2.3 three locking protocol

Deadlock and livelock 3.3

3.3.1 Livelock

3.3.2 Deadlock

3.3.3 Deadlock Detection and released

Fourth, the scheduling concurrent serializable

4.1 serial schedule with concurrent scheduling

4.1.1 serial schedule

4.1.2 Concurrent scheduling

4.2 conflict serializable

Five, two-phase locking protocol

Six multi-block size

6.1 size

6.2 intent locks


I. Overview of the transaction

1.1 Definitions

The transaction is a sequence of user-defined database operations, these operations are either all do or do not do the whole, an indivisible unit of work

A 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 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

2.2.1 loss modification

2.2.2 to read "dirty" data

2.2.3 Non-repeatable read

cause

  • 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

 

A sequence of activities airline reservation system: Example

① A ticket (transaction T1) read out a balance of flight tickets A, setting A = 16;

② ticket acetate (transaction T2) read out the balance on the same flight tickets A, is also 16;

③ A ticket to sell a ticket, modify the balance of A ← A-1, so the A 15, A written back to the database;

④ B ticket also sell a ticket, modify the balance of A ← A-1, so the A 15, A written back to the database

The results clearly sell two tickets, ticket database only reduce the balance of 1

This is called a database inconsistency, caused by concurrent operation

In the concurrent operation, the scheduling of T1, T2 of the two transactions is random sequence of operations. 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 to use the scheduling of concurrent operations in the right way, the execution of a transaction not subject to interference from other transactions

The main technical concurrency control: the blockade, timestamp, optimistic Control Act, multi-version concurrency control

 

 

Third, the blockade

封锁即事务在对某个数据对象操作之前,先向系统发出加锁请求,加锁后事务对该数据对象有了一定的控制权,在事务释放它的锁之前,其他事务不能更新该数据对象

3.1基本锁类型

排它锁又称写锁或X,若事务 T对数据对象 A加上X锁,则只允许 T读取和修改 A,任何其它事务都不能再对 A读取和修改,直到 T释放A上的锁

共享锁又称读锁或S,若事务 T对数据对象 A加上 S锁,则事务 T可以读 A但不能修改 A,其它事务只能再对 A读取,而不能修改,直到 T释放 A上的锁2.2封锁协议

 

3.2封锁协议

3.2.1一级封锁协议

事务T在修改数据对象R之前必须先对其加X锁,直到事务结束才释放

作用:一级封锁协议可防止丢失修改,并保证事务T是可恢复的

说明:在一级封锁协议中,如果仅仅是读数据不对其进行修改,是不需要加锁的,所以它不能保证可重复读和不读“脏”数据

3.2.2二级封锁协议

在一级封锁协议的基础上,事务T在读取数据对象R之前必须先对其加S锁,但是读完后即可释放S锁

作用:二级封锁协议可以防止丢失修改和读“脏”数据

说明:在二级封锁协议中,由于读完数据后即可释放S锁,所以它不能保证可重复读

3.2.3三级封锁协议

在一级封锁协议的基础上,事务T在读取数据R之前必须先对其加S锁,直到事务结束才释放

作用:三级封锁协议可防止丢失修改、读脏数据和不可重复读

三级协议的主要区别

  • 什么操作需要申请封锁以及何时释放锁(即持锁时间)
  • 封锁协议级别越高,一致性程度越高

 

 

X

S

一致性保证

 

操作结束释放

事务结束释放

操作结束释放

事务结束释放

不丢失

修改

不读“脏”数据

可重复

一级封锁协议

 

 

 

 

 

二级封锁协议

 

 

 

三级封锁协议

 

 

 

3.3活锁与死锁

3.3.1活锁

又称饥饿,是某个事务因等待锁而处于无限期等待状态

避免活锁:先来先服务,当多个事务请求封锁同一数据对象时,按请求封锁的先后次序对这些事务排队,该数据对象上的锁一旦释放,首先批准申请队列中第一个事务获得锁

3.3.2死锁

两个或两个以上的事务之间的循环等待现象

死锁的预防

1、一次性封锁法

每个事务必须一次将所有要使用的数据对象全部加锁后,在实际执行实务操作,否则事务不进行任何实际行动也不封锁任何数据

存在问题

  • 使数据的上锁时间增长,降低系统的并发度
  • 事先确定事务要封锁的数据对象很难

2、顺序封锁法

预先对数据对象规定一个封锁顺序,所有事务都按这个顺序实行封锁

存在的问题

  • 维护成本很高,数据库系统中封锁的数据对象极多,并且随数据的插入、删除等操作而不断地变化,要维护这样的资源的封锁顺序非常困难

上述两种方法虽然都可以有效地预防死锁,但都存在一些问题,因此真正实施起来并不方便。所以预防死锁的策略不很适合数据库的特点,DBMS普遍采用诊断死锁并解除的方法

3.3.3死锁的检测与解除

1、死锁的检测

数据库系统通常采用超时或事务等待图法发现死锁

2、解除死锁

选择一个或多个处于死锁状态的事务,将其撤消并释放这些事务持有的所有的锁,从而打破了循环等待条件,解除死锁,使其它事务能够继续运行

 

 

四、并发调度的可串行化

4.1串行调度与并发调度

4.1.1串行调度

一组事务的串行调度是指这些事务一个接一个地执行,其中每个事务都在上一个事务(如果有)完全结束之后才开始执行

对于一组事务,串行调度总是正确的

对于 n个事务,存在 n! 个不同的串行调度,可能导致不同的结果

4.1.2并发调度

一组事物的并发调度是指这些事务中至少有两个事务都开始了它们的执行,并且都尚未结束

并发调度正确性规则:一组事物的并发调度是正确的,当且仅当调度的执行结果与某一个串行调度的执行结果相同,此时称并发调度是可串行化的

 

4.2冲突可串行化

冲突操作:是指不同的事务对同一数据的读写操作和写写操作

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

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

不能交换的动作包括一事务的两个操作不同事务的冲突操作

一个调度Sc在保证冲突操作的次序不变的情况下,通过交换两个事务不冲突操作的次序得到另一个调度Sc',如果Sc'是串行的,称调度Sc是冲突可串行化的调度若一个调度是冲突可串行化,则一定是可串行化的调度

冲突可串行化调度是可串行化调度的充分条件,不是必要条件

 

 

五、两阶段锁协议

“两阶段”锁的含义是事务分为两个阶段

第一阶段是获得封锁,也称为扩展阶段

  • 事务可以申请获得任何数据项上的任何类型的锁,但是不能释放任何锁

第二阶段是释放封锁,也称为收缩阶段

  • 事务可以释放任何数据项上的任何类型的锁,但是不能再申请任何锁

两阶段锁协议的正确性

  • 若所有事务都遵守两段封锁协议,则对这些事务的任何并发调度策略都是可串行化的
  • 两阶段锁协议是并发控制正确性的充分条件,但不是必要条件。即若所有事务都遵守两阶段锁协议,则这些事务的任何并发调度都是可串行化的,反之,一个并发调度是可串行化的,不一定所有事务都遵守两阶段锁协议

两段锁协议与防止死锁的一次封锁法

  • 一次封锁法要求每个事务必须一次将所有要使用的数据全部加锁,否则就不能继续执行,因此一次封锁法遵守两段锁协议
  • 但是两段锁协议并不要求事务必须一次将所有要使用的数据全部加锁,因此遵守两段锁协议的事务可能发生死锁

 

 

六、多封锁粒度

6.1粒度

封锁对象的大小称为粒度,可以是数据库、表、记录、字段等

封锁粒度与系统的并发度和并发控制的开销密切相关。封锁的粒度越大,数据库所能够封锁的数据单元就越少,并发度就越小,系统开销也越小;封锁的粒度越小,并发度越高,但系统开销也就越大


多粒度封锁是同时支持多种封锁粒度供不同事务选择的封锁方法

  • 依赖的数据结构——多粒度树
  1. 将数据库中的数据对象按相互关系和粒度大小组织成的树型结构,其中根结点表示最大数据粒度,通常为整个数据库,叶结点表示最小数据粒度

多粒度封锁协议允许对多粒度树中的每个结点独立地加锁,并且对每一个结点加锁隐含着对其后裔结点也加以同样的锁

  • 在多粒度封锁中一个数据对象可能以两种方式封锁:显式封锁隐式封锁
  1. 由事务直接加到数据对象上的封锁称为显式封锁
  2. 因上级结点加锁而引起下级对象被封锁,称为隐式封锁
  3. 显式封锁和隐式封锁的效果是一样

系统检查封锁冲突时要检查显式封锁还要检查隐式封锁

多粒度封锁方法

为对某数据对象加锁,系统要检查

  • 该对象有无显式封锁与之冲突
  • 该对象的上级结点有无显式封锁与之冲突
  • 该对象的下级结点有无显式封锁与之冲突

当无任何冲突时方能加锁成功

 

6.2意向锁

对任一结点加锁时,必须先对其上级结点加意向锁

目的:提高对某个数据对象加锁时系统的检查效率

作用:减少加锁时的封锁冲突检查工作量。只需检查上级结点与本结点是否已加了不相容的锁,并通过本结点的意向锁了解下级结点是否有不相容的锁,从而不必再检查下级结点

三种意向锁

  • 意向共享锁(IS锁):如果对一个数据对象加IS锁,表示它的后裔结点拟(意向)加S锁
  • IX锁:如果对一个数据对象加IX锁,表示它的后裔结点拟(意向)加X锁
  • SIX锁:如果对一个数据对象加SIX锁,表示对它加S锁,再加IX锁,即SIX = S + IX

锁的强度 指它对其他锁的排斥程度,一个事务在申请封锁时以强锁代替弱锁是安全的,反之则不然

意向锁的封锁和释放顺序

  • 申请封锁时应该按自上而下的次序进行
  • 释放封锁时则应该按自下而上的次序进行

Guess you like

Origin blog.csdn.net/zjuwxx/article/details/90346903
Recommended