目录
Rigorous 2PL and Conservative 2PL are NOT the same thing
三、最优并发控制(Optimistic Concurrency Control,OCC)
四、数据的粒度(Granularity of Data Item)
(二)检查点(checkpoint),重做(redo),撤销(undo)
一、锁带来的问题
即使是两相锁(2PL),使用不严格也会导致这些问题:
(一)级联回滚(Cascading Rollback)
A transaction (T1) causes a failure and a rollback must be performed. Other transactions dependent on T1's actions must also be rollbacked, thus causing a cascading effect.
One transaction's failure causes many to fail.
Solution:
![](https://img-blog.csdnimg.cn/d768cd5f154948a8bb08e7050210b7db.png)
严格执行2PL的示例:
![](https://img-blog.csdnimg.cn/efbeb33660b945b38dcf17d7cf7397f1.png)
(二)死锁(Deadlock)
Definition:2 (or more) transactions are each waiting for locks held by the other to be released
这是个“两只小羊过独木桥”的伤心故事。。。当两个事务都在等待对方解锁时,它们再也不能做任何事,除非其中某个事务突然终止,否则它们等会永远等下去
General techniques:
Timeout(超时)
不论死锁是否出现都有可能触发它
![](https://img-blog.csdnimg.cn/5d126945537349fbbf711f407f64fea1.png)
死锁预防(Deadlock Prevention)
杜绝死锁发生的可能
Def:DBMS looks ahead to see if the transaction would cause a deadlock and never allows a deadlock to occur.
Conservative 2PL
Def:All data items have to be locked at the beginning of a transaction.
Rigorous 2PL and Conservative 2PL are NOT the same thing
Rigorous:keep all locks until the end
Conservative:acquire all locks at the beginning
a safest 2PL = Rigorous + Conservative
二、时间戳(Timestamping)
A concurrency control mechanism.
- Transactions are ordered globally so that older transactions (i.e. with smaller timestamps) get priority in the event of conflict.
- Read/write proceeds only if last update on that data item was carried out by an older transaction.
- Otherwise, transaction requesting read/write is restarted and given a new timestamp.
- No locks so no deadlock.
-
There are also timestamps for data items:
-
When transaction T asks to read/write a data item x, database will compare T’s timestamp with the read/write timestamp of x, then decide whether to proceed T or abort/restart T.
![](https://img-blog.csdnimg.cn/0ee9b1cb328c4d1d98c2cddb620b94ba.png)
三、最优并发控制(Optimistic Concurrency Control,OCC)
- Assumption: conflict is rare → more efficient to let transactions proceed without delays to ensure serialisability. (e.g., All transactions are readers)
- At commit, a check is made to determine whether conflict has occurred.
- Potentially allows greater concurrency than traditional protocols.
Phase:
四、数据的粒度(Granularity of Data Item)
![](https://img-blog.csdnimg.cn/c7f2f1d935824c62bd32b0459d970873.png)
五、数据恢复(Data Recovery)
There are several facilities:
(一)日志文件(Log File)
![](https://img-blog.csdnimg.cn/5b897d6df9df4d198e19c491b37cbdff.png)
![](https://img-blog.csdnimg.cn/9404f2da56ae48ac992dcb108d365356.png)
(二)检查点(checkpoint),重做(redo),撤销(undo)
- Point of synchronization between database and log file. All buffers are force-written to secondary storage.
-
Checkpoint record is created containing identifiers of all activetransactions.
-
When failure occurs:leave transactions committed before the checkpoint alone,they have done their work;redo all transactions that committed since the checkpoint ,即前滚(rollforward);
E.g. A number of transactions being processed from t0 until tf when system had a power failure. At tc a checkpoint was done. Explain for each transactions in the figure below how recovery is done.
Summary
注:部分内容参考自图书《数据库系统-设计、实现与管理(基础篇)》