CMU Database Systems - Two-phase Locking

首先锁是用来做互斥的,解决并发执行时的数据不一致问题

如图会导致,不可重复读

如果这里用lock就可以解决,数据库里面有个LockManager来作为master,负责锁的记录和授权

数据库里面的基本的锁类型,

其实就是读锁,写锁

但是如果光是有读写锁,只能解决当个操作互斥和正确,无法解决transaction的正确

所以我们需要一个事务级别的锁,就是2PL,两阶段提交

最核心的想法,在growing阶段需要拿到所有需要的锁,否则就会block;shrinking阶段,不能去增加锁,只能释放锁

2PL在shrinking阶段是可以逐个去释放锁的,这样会有cascding aborts问题

因为你释放部分锁的时候,其他的事务就会看到你的改动,但最终你abort,那么所有相关的事务由于脏读也必须要abort

2PL有如下的问题,

首先,2PL是充分不必要条件,不满足2PL并不一定会导致调度问题,所以2PL限制了并发

第二,由于脏读导致的Cascding abort,这个的解决很直接,Strict 2PL,Shrinking阶段不会逐步释放锁,最后一起释放,这样就不会脏读了,这个方法会进一步限制并发,谈不上优雅

下面看一组例子,

非2PL,读到的是A,B的中间结果,所以会发生不一致;2PL,解决了不一致问题;Strict 2PL,明显进一步限制了并发,几乎就是顺序执行

事务还有一个问题,死锁

死锁就是发生锁环了,两种解决方法,

Detection和Prevention,detection就是检测有没有环,如果有环就处理;Prevention就是预先判断是不是会形成环,如果会就拒绝请求

死锁Detection,生成waits-for图,如果有环,就说明有死锁

出现死锁,解决从策略就是挑一个进行重启或abort

挑选的策略就是代价更低,然后挑出合适的victim后,就是要进行处理

处理的时候,可以分为完全Rollback和部分Rollback,因为有时候Rollback到不持有这锁就可以解决死锁的问题,不用完全的rollback

prevention的策略如下,prevention的依据就是时间,要不新的等,要不老的等

锁粒度

对数据库加锁可以在各个粒度上,

在树上任一节点加锁,意味着对所有子节点也持有锁

意向锁,intention lock

比如你在要给table加锁的时候,你先要确认table底下的所有tuple,attr是否有锁,这样很低效

所以意向锁就是一个flag,标识子节点上是否有锁

意向锁分为几类,

读写意向锁,很好理解,就是表示子节点是否有读写锁

SIX,Shared Intention Exclusive,首先加Shared锁,这样可以扫描全表,然后加IX锁,需要更改其中某些tuple

例子,

原文地址:https://www.cnblogs.com/fxjwind/p/11082695.html

时间: 2024-11-06 09:47:43

CMU Database Systems - Two-phase Locking的相关文章

CMU Database Systems - Sorting,Aggregation,Join

Sorting 排序如果可在内存里面排,用经典的排序算法就ok,比如快排 问题在于,数据表中的的数据是很多的,没法一下都放到内存里面进行排序 所以就需要用到,外排,多路并归排序 看下最简单的,2路并归排序, 设文件分为N个page,memory中一次最多可以放入B个pages 所以在sort过程,一次性可以载入B个page,在内存中page内排序,写回disk,称为一轮,run那么如果一共N个page,需要N/B+1个run 在merge过程,如果双路并归排序,只需要用到3个page的buffe

22C4400 – Database Systems Project

22C4400 – Database Systems Project – Initial 1/28/2019Project ScopeRaman Aravamudhan RSA-22C4400-S19-PROJECT-SCOPEDepartment of Computer Science Page 1 of 5 Version 1.1Note: This document is intentionally incomplete and is in non-standardformat; as p

Harvard数据库课程CS 265: Research Topics in Database Systems

CS 265: Research Topics in Database Systems Announcements Quiz 3 will be posted. Good luck! Quiz 2 has been posted. Good luck! Here is the History of a Paper that was mentioned in the Project handout. Please complete this Paper Survey for the first t

CHAPTER 1 Introduction to database (第一章 数据库简介)

Chaper  Objectives  (章节目标) In this chapter you will learn:   (在这一章节中,你将学习) 1. Some common uses of database systems.   (数据库系统的一些普通扩法) 2.The characteristics of file-based systems. (基于文件系统的一些特点.) 3. The problems with the file-based systems.  (基于文件系统拥有的一

Avoiding PostgreSQL database corruption

TL;DR: Don't ever set fsync=off, don't kill -9 the postmaster then deletepostmaster.pid, don't run PostgreSQL on network file systems. Reports of database corruption on the PostgreSQL mailing list are uncommon, but hardly rare. While a few data corru

Large, Interconnected Data Belongs to a Database

Large, Interconnected Data Belongs to a Database Diomidis Spinellis iF YOUR APPLiCATiON is going to handle a large, persistent, interconnected set of data elements, don't hesitate to store it in a relational database. In the past, RDBMSs used to be e

Database Overview

DBMS(Database-Management System): a collection of interrelated data and set of programs to access those data. Part 1: Database-System Applications 1. Enterprise Information 2. Banking and Finance 3. Universities 4. Airlines 5. Telecommunication Part

[转]Amazon DynamoDB – a Fast and Scalable NoSQL Database Service Designed for Internet Scale Applications

This article is from blog of Amazon CTO Werner Vogels. -------------------- Today is a very exciting day as we release Amazon DynamoDB, a fast, highly reliable and cost-effective NoSQL database service designed for internet scale applications. Dynamo

CHAPTER 2 Database Environment

Chapter Objectives   (章节目标) In this chapter you will learn:  (在本章中,你将学习:) The purpose and origin of the three-level database architecture.  (三层数据库的架构和起源.) The contents of the external, conceptual, and internal levels. (外部.概念.内部层次的内容.) The purpose of