Presentation is loading. Please wait.

Presentation is loading. Please wait.

An Introduction to Database System An Introduction to Database System

Similar presentations


Presentation on theme: "An Introduction to Database System An Introduction to Database System"— Presentation transcript:

1 An Introduction to Database System An Introduction to Database System
数据库系统概论 An Introduction to Database System 并发控制 An Introduction to Database System

2 An Introduction to Database System
问题的产生 多用户数据库系统的存在 允许多个用户同时使用的数据库系统 飞机定票数据库系统 银行数据库系统 特点:在同一时刻并发运行的事务数可达数百个 An Introduction to Database System

3 An Introduction to Database System
问题的产生(续) 不同的多事务执行方式 (1)事务串行执行 每个时刻只有一个事务运行,其他事务必须等到这个事务结束以后方能运行 不能充分利用系统资源,发挥数据库共享资源的特点 T1 T2 T3 事务的串行执行方式 An Introduction to Database System

4 An Introduction to Database System
问题的产生(续) (2)交叉并发方式(Interleaved Concurrency) 在单处理机系统中,事务的并行执行是这些并行事务的并行操作轮流交叉运行 单处理机系统中的并行事务并没有真正地并行运行,但能够减少处理机的空闲时间,提高系统的效率 An Introduction to Database System

5 An Introduction to Database System
问题的产生(续) 事务的交叉并发执行方式 An Introduction to Database System

6 An Introduction to Database System
问题的产生(续) (3)同时并发方式(simultaneous concurrency) 多处理机系统中,每个处理机可以运行一个事务,多个处理机可以同时运行多个事务,实现多个事务真正的并行运行 An Introduction to Database System

7 An Introduction to Database System
问题的产生(续) 事务并发执行带来的问题 会产生多个事务同时存取同一数据的情况 可能会存取和存储不正确的数据,破坏事务一致性和数据库的一致性 An Introduction to Database System

8 An Introduction to Database System
4.1 并发控制概述 并发控制机制的任务 对并发操作进行正确调度 保证事务的隔离性 保证数据库的一致性 An Introduction to Database System

9 An Introduction to Database System
并发控制概述(续) 并发操作带来数据的不一致性实例 [例1]飞机订票系统中的一个活动序列 ① 甲售票点(甲事务)读出某航班的机票余额A,设A=16; ② 乙售票点(乙事务)读出同一航班的机票余额A,也为16; ③ 甲售票点卖出一张机票,修改余额A←A-1,所以A为15,把A写回数据库; ④ 乙售票点也卖出一张机票,修改余额A←A-1,所以A为15,把A写回数据库 结果明明卖出两张机票,数据库中机票余额只减少1 T1的修改被T2覆盖了! An Introduction to Database System

10 An Introduction to Database System
并发控制概述(续) 这种情况称为数据库的不一致性,是由并发操作引起的。 在并发操作情况下,对甲、乙两个事务的操作序列的调度是随机的。 若按上面的调度序列执行,甲事务的修改就被丢失。 原因:第4步中乙事务修改A并写回后覆盖了甲事务的修改 An Introduction to Database System

11 An Introduction to Database System
并发控制概述(续) 并发操作带来的数据不一致性 丢失修改(Lost Update) 不可重复读(Non-repeatable Read) 读“脏”数据(Dirty Read) 记号 R(x):读数据x W(x):写数据x An Introduction to Database System

12 An Introduction to Database System
1. 丢失修改 两个事务T1和T2读入同一数据并修改,T2的提交结果破坏了T1提交的结果,导致T1的修改被丢失。 上面飞机订票例子就属此类 An Introduction to Database System

13 An Introduction to Database System
丢失修改(续) T1 T2 ① R(A)=16 R(A)=16 ③ A←A-1 W(A)=15 A←A-1 丢失修改 An Introduction to Database System

14 An Introduction to Database System
2. 不可重复读 不可重复读是指事务T1读取数据后,事务T2 执行更新操作,使T1无法再现前一次读取结果。 An Introduction to Database System

15 An Introduction to Database System
不可重复读(续) 不可重复读包括三种情况: (1)事务T1读取某一数据后,事务T2对其做了修改,当事务T1再次读该数据时,得到与前一次不同的值 An Introduction to Database System

16 An Introduction to Database System
不可重复读(续) 例如: T1 T2 ① R(A)=50 R(B)=100 求和=150 B←B*2 W(B)=200 ③ R(A)=50 R(B)=200 和=250 (验算不对) T1读取B=100进行运算 T2读取同一数据B,对其进行修改后将B=200写回数据库。 T1为了对读取值校对重读B,B已为200,与第一次读取值不一致 An Introduction to Database System 不可重复读

17 An Introduction to Database System
不可重复读(续) (2)事务T1按一定条件从数据库中读取了某些数据记录后,事 务T2删除了其中部分记录,当T1再次按相同条件读取数据 时,发现某些记录消失了 (3)事务T1按一定条件从数据库中读取某些数据记录后,事务T2插入了一些记录,当T1再次按相同条件读取数据时,发现多了一些记录。 后两种不可重复读有时也称为幻影现象(Phantom Row) An Introduction to Database System

18 An Introduction to Database System
3. 读“脏”数据 读“脏”数据是指: 事务T1修改某一数据,并将其写回磁盘 事务T2读取同一数据后,T1由于某种原因被撤销 这时T1已修改过的数据恢复原值,T2读到的数据就与数据库中的数据不一致 T2读到的数据就为“脏”数据,即不正确的数据 An Introduction to Database System

19 An Introduction to Database System
读“脏”数据(续) 例如 T1 T2 ① R(C)=100 C←C*2 W(C)=200 R(C)=200 ③ROLLBACK C恢复为100 T1将C值修改为200,T2读到C为200 T1由于某种原因撤销,其修改作废,C恢复原值100 这时T2读到的C为200,与数据库内容不一致,就是“脏”数据 读“脏”数据 An Introduction to Database System

20 An Introduction to Database System
并发控制概述(续) 数据不一致性:由于并发操作破坏了事务的隔离性 并发控制就是要用正确的方式调度并发操作,使一个用户事务的执行不受其他事务的干扰,从而避免造成数据的不一致性 An Introduction to Database System

21 An Introduction to Database System
并发控制概述(续) 并发控制的主要技术 封锁(Locking) 时间戳(Timestamp) 乐观控制法 商用的DBMS一般都采用封锁方法 An Introduction to Database System

22 An Introduction to Database System
4.2 封锁 什么是封锁 基本封锁类型 锁的相容矩阵 An Introduction to Database System

23 An Introduction to Database System
什么是封锁 封锁就是事务T在对某个数据对象(例如表、记录等)操作之前,先向系统发出请求,对其加锁 加锁后事务T就对该数据对象有了一定的控制,在事务T释放它的锁之前,其它的事务不能更新此数据对象。 An Introduction to Database System

24 An Introduction to Database System
基本封锁类型 一个事务对某个数据对象加锁后究竟拥有什么样的控制由封锁的类型决定。 基本封锁类型 排它锁(Exclusive Locks,简记为X锁) 共享锁(Share Locks,简记为S锁) An Introduction to Database System

25 An Introduction to Database System
排它锁 排它锁又称为写锁 若事务T对数据对象A加上X锁,则只允许T读取和 修改A,其它任何事务都不能再对A加任何类型的 锁,直到T释放A上的锁 保证其他事务在T释放A上的锁之前不能再读取和 修改A An Introduction to Database System

26 An Introduction to Database System
共享锁 共享锁又称为读锁 若事务T对数据对象A加上S锁,则其它事务只能 再对A加S锁,而不能加X锁,直到T释放A上的S 锁 保证其他事务可以读A,但在T释放A上的S锁之前 不能对A做任何修改 An Introduction to Database System

27 An Introduction to Database System
锁的相容矩阵 Y=Yes,相容的请求 N=No,不相容的请求 T1 T2 X S - N Y An Introduction to Database System

28 An Introduction to Database System
锁的相容矩阵(续) 在锁的相容矩阵中: 最左边一列表示事务T1已经获得的数据对象上的锁的类型,其中横线表示没有加锁。 最上面一行表示另一事务T2对同一数据对象发出的封锁请求。 T2的封锁请求能否被满足用矩阵中的Y和N表示 Y表示事务T2的封锁要求与T1已持有的锁相容,封锁请求可以满足 N表示T2的封锁请求与T1已持有的锁冲突,T2的请求被拒绝 An Introduction to Database System

29 An Introduction to Database System
使用封锁机制解决丢失修改问题 例: 没有丢失修改 T1 T2 ① Xlock A ② R(A)=16 Xlock A ③ A←A-1 等待 W(A)=15 Commit Unlock A 获得Xlock A R(A)=15 A←A-1 W(A)=14 事务T1在读A进行修改之前先对A加X锁 当T2再请求对A加X锁时被拒绝 T2只能等待T1释放A上的锁后T2获得对A的X锁 这时T2读到的A已经是T1更新过的值15 T2按此新的A值进行运算,并将结果值A=14送回到磁盘。避免了丢失T1的更新。 An Introduction to Database System

30 An Introduction to Database System
使用封锁机制解决不可重复读问题 T1 T2 ① Slock A Slock B R(A)=50 R(B)=100 求和=150 Xlock B 等待 ③ R(A)=50 Commit Unlock A Unlock B 获得XlockB B←B*2 W(B)=200 可重复读 事务T1在读A,B之前,先对A,B加S锁 其他事务只能再对A,B加S锁,而不能加X锁,即其他事务只能读A,B,而不能修改 当T2为修改B而申请对B的X锁时被拒绝只能等待T1释放B上的锁 T1为验算再读A,B,这时读出的B仍是100,求和结果仍为150,即可重复读 T1结束才释放A,B上的S锁。T2才获得对B的X锁 An Introduction to Database System

31 An Introduction to Database System
使用封锁机制解决读“脏”数据问题 T1 T2 ① Xlock C R(C)=100 C←C*2 W(C)=200 Slock C 等待 ③ ROLLBACK (C恢复为100) Unlock C 获得Slock C Commit C 不读“脏”数据 事务T1在对C进行修改之前,先对C加X锁,修改其值后写回磁盘 T2请求在C上加S锁,因T1已在C上加了X锁,T2只能等待 T1因某种原因被撤销,C恢复为原值100 T1释放C上的X锁后T2获得C上的S锁,读C=100。避免了T2读“脏”数据 An Introduction to Database System

32 An Introduction to Database System
4.3 活锁和死锁 封锁技术可以有效地解决并行操作的一致性问题,但也带来一些新的问题 死锁 活锁 An Introduction to Database System

33 An Introduction to Database System
4.3.1 活锁 事务T1封锁了数据R 事务T2又请求封锁R,于是T2等待。 T3也请求封锁R,当T1释放了R上的封锁之后系统首先批准了T3的请求,T2仍然等待。 T4又请求封锁R,当T3释放了R上的封锁之后系统又批准了T4的请求…… T2有可能永远等待,这就是活锁的情形 An Introduction to Database System

34 An Introduction to Database System
活锁(续) 活 锁 An Introduction to Database System

35 An Introduction to Database System
活锁(续) 避免活锁:采用先来先服务的策略 当多个事务请求封锁同一数据对象时 按请求封锁的先后次序对这些事务排队 该数据对象上的锁一旦释放,首先批准申请队列中第一个事务获得锁 An Introduction to Database System

36 An Introduction to Database System
死锁 事务T1封锁了数据R1 T2封锁了数据R2 T1又请求封锁R2,因T2已封锁了R2,于是T1等待T2释放R2上的锁 接着T2又申请封锁R1,因T1已封锁了R1,T2也只能等待T1释放R1上的锁 这样T1在等待T2,而T2又在等待T1,T1和T2两个事务永远不能结束,形成死锁 An Introduction to Database System

37 An Introduction to Database System
死锁(续) T1 T2 lock R1 Lock R2 Lock R2. 等待 Lock R1 死 锁 An Introduction to Database System

38 An Introduction to Database System
解决死锁的方法 两类方法 1. 预防死锁 2. 死锁的诊断与解除 An Introduction to Database System

39 An Introduction to Database System
1. 死锁的预防 产生死锁的原因是两个或多个事务都已封锁了一些数据对象,然后又都请求对已为其他事务封锁的数据对象加锁,从而出现死等待。 预防死锁的发生就是要破坏产生死锁的条件 An Introduction to Database System

40 An Introduction to Database System
死锁的预防(续) 预防死锁的方法 一次封锁法 顺序封锁法 An Introduction to Database System

41 An Introduction to Database System
(1)一次封锁法 要求每个事务必须一次将所有要使用的数据全部加锁,否则就不能继续执行 存在的问题 降低系统并发度 难于事先精确确定封锁对象 An Introduction to Database System

42 An Introduction to Database System
(2)顺序封锁法 顺序封锁法是预先对数据对象规定一个封锁顺序,所有事务都按这个顺序实行封锁。 顺序封锁法存在的问题 维护成本 数据库系统中封锁的数据对象极多,并且在不断地变化。 难以实现:很难事先确定每一个事务要封锁哪些对象 An Introduction to Database System

43 An Introduction to Database System
死锁的预防(续) 结论 在操作系统中广为采用的预防死锁的策略并不很适合数据库的特点 DBMS在解决死锁的问题上更普遍采用的是诊断并解除 死锁的方法 An Introduction to Database System

44 An Introduction to Database System
2. 死锁的诊断与解除 死锁的诊断 超时法 事务等待图法 An Introduction to Database System

45 An Introduction to Database System
(1) 超时法 如果一个事务的等待时间超过了规定的时限,就认为发生了死锁 优点:实现简单 缺点 有可能误判死锁 时限若设置得太长,死锁发生后不能及时发现 An Introduction to Database System

46 An Introduction to Database System
(2)等待图法 用事务等待图动态反映所有事务的等待情况 事务等待图是一个有向图G=(T,U) T为结点的集合,每个结点表示正运行的事务 U为边的集合,每条边表示事务等待的情况 若T1等待T2,则T1,T2之间划一条有向边,从T1指向T2 An Introduction to Database System

47 An Introduction to Database System
等待图法(续) 事务等待图 图(a)中,事务T1等待T2,T2等待T1,产生了死锁 图(b)中,事务T1等待T2,T2等待T3,T3等待T4,T4又等待T1,产生了死锁 图(b)中,事务T3可能还等待T2,在大回路中又有小的回路 An Introduction to Database System

48 An Introduction to Database System
等待图法(续) 并发控制子系统周期性地(比如每隔数秒)生成事务等待图,检测事务。如果发现图中存在回路,则表示系统中出现了死锁。 An Introduction to Database System

49 An Introduction to Database System
死锁的诊断与解除(续) 解除死锁 选择一个处理死锁代价最小的事务,将其撤消 释放此事务持有的所有的锁,使其它事务能继续运行下去 An Introduction to Database System

50 An Introduction to Database System
4.4 并发调度的可串行性 DBMS对并发事务不同的调度可能会产生不同的结果 什么样的调度是正确的? An Introduction to Database System

51 An Introduction to Database System
4.4.1 可串行化调度 可串行化(Serializable)调度 多个事务的并发执行是正确的,当且仅当其结果与按某一次序串行地执行这些事务时的结果相同 可串行性(Serializability) 是并发事务正确调度的准则 一个给定的并发调度,当且仅当它是可串行化的,才认为是正确调度 An Introduction to Database System

52 An Introduction to Database System
可串行化调度(续) [例]现在有两个事务,分别包含下列操作: 事务T1:读B;A=B+1;写回A 事务T2:读A;B=A+1;写回B 现给出对这两个事务不同的调度策略 An Introduction to Database System

53 An Introduction to Database System
串行化调度,正确的调度 T1 T2 Slock B Y=R(B)=2 Unlock B Xlock A A=Y+1=3 W(A) Unlock A Slock A X=R(A)=3 Xlock B B=X+1=4 W(B) 假设A、B的初值均为2。 按T1→T2次序执行结果为A=3,B=4 串行调度策略,正确的调度 An Introduction to Database System 串行调度(a)

54 An Introduction to Database System
串行化调度,正确的调度 T1 T2 Slock A X=R(A)=2 Unlock A Xlock B B=X+1=3 W(B) Unlock B Slock B Y=R(B)=3 Xlock A A=Y+1=4 W(A) 假设A、B的初值均为2。 T2→T1次序执行结果为B=3,A=4 串行调度策略,正确的调度 An Introduction to Database System 串行调度(b)

55 An Introduction to Database System
不可串行化调度,错误的调度 T1 T2 Slock B Y=R(B)=2 Slock A X=R(A)=2 Unlock B Unlock A Xlock A A=Y+1=3 W(A) Xlock B B=X+1=3 W(B) 执行结果与(a)、(b)的结果都不同 是错误的调度 An Introduction to Database System 不可串行化的调度

56 An Introduction to Database System
可串行化调度,正确的调度 T1 T2 Slock B Y=R(B)=2 Unlock B Xlock A Slock A A=Y+1=3 等待 W(A) Unlock A X=R(A)=3 Xlock B B=X+1=4 W(B) 执行结果与串行调度(a)的执行结果相同 是正确的调度 An Introduction to Database System 可串行化的调度

57 An Introduction to Database System
冲突可串行化调度 可串行化调度的充分条件 一个调度Sc在保证冲突操作的次序不变的情况下,通过交换两个事务不冲突操作的次序得到另一个调度Sc‘,如果Sc’是串行的,称调度Sc为冲突可串行化的调度 一个调度是冲突可串行化,一定是可串行化的调度 An Introduction to Database System

58 An Introduction to Database System
冲突可串行化调度(续) 冲突操作 冲突操作是指不同的事务对同一个数据的读写操作和写写操作 Ri (x)与Wj(x) /* 事务Ti读x,Tj写x*/ Wi(x)与Wj(x) /* 事务Ti写x,Tj写x*/ 其他操作是不冲突操作 不同事务的冲突操作和同一事务的两个操作不能交换(Swap) An Introduction to Database System

59 An Introduction to Database System
冲突可串行化调度(续) [例]今有调度Sc1=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B) 把w2(A)与r1(B)w1(B)交换,得到: r1(A)w1(A)r2(A)r1(B)w1(B)w2(A)r2(B)w2(B) 再把r2(A)与r1(B)w1(B)交换: Sc2=r1(A)w1(A)r1(B)w1(B)r2(A)w2(A)r2(B)w2(B) Sc2等价于一个串行调度T1,T2,Sc1冲突可串行化的调度 An Introduction to Database System

60 An Introduction to Database System
冲突可串行化调度(续) 冲突可串行化调度是可串行化调度的充分条件,不是必要条件。还有不满足冲突可串行化条件的可串行化调度。 [例]有3个事务 T1=W1(Y)W1(X),T2=W2(Y)W2(X),T3=W3(X) 调度L1=W1(Y)W1(X)W2(Y)W2(X) W3(X)是一个串行调度。 调度L2=W1(Y)W2(Y)W2(X)W1(X)W3(X)不满足冲突可串行化。但是调度L2是可串行化的,因为L2执行的结果与调度L1相同,Y的值都等于T2的值,X的值都等于T3的值 An Introduction to Database System


Download ppt "An Introduction to Database System An Introduction to Database System"

Similar presentations


Ads by Google