كل المقررات

الرئيسية / مقرر قاعدة البيانات / الفصل 10 الجزء 1

الفصل 10 (الجزء 1): Concurrency Control

Locks and two-phase locking

على طول: الـ locks بتمنع التعارض بين الـ transactions، وقاعدة الـ two-phase locking.

فيديو المحاضرة

فيديو المحاضرة

بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.

رسوم متحركة

الـ lock على item

أمثلة SQL

أمثلة قصيرة وبسيطة

الكود قصير والنتيجة اتجرّبت فعلاً. عدّل في المحرر واضغط شغّل.

المثال ١: جدول الـ locks

Sample output (press Run to produce your own):
item | holder | mode
-----+--------+-----
X    | T1     | S   
X    | T2     | S   
Y    | T1     | X   
(3 rows)

المثال ٢: هل الطلب متوافق؟

Sample output (press Run to produce your own):
item | answer 
-----+--------
X    | granted
Y    | wait   
(2 rows)

المثال ٣: السحب الصح مع قفل صريح

Sample output (press Run to produce your own):
balance
-------
900    
(1 row)

افتح الموضوع المقابل في صفحة LiU TDDD12

اختبار قصير

اختبر فهمك

1. How many transactions can hold a shared (S) lock on one item?

2. An exclusive (X) lock is needed for:

3. Two-phase locking means:

4. 2PL guarantees:

5. Strict 2PL keeps X locks until:

تمارين برمجة

جرّب بإيدك

التمرين 1

ضيف في lock_table إن T3 ماسكة X على Z، وبعدين اعرض مين ماسك إيه.

Add to the lock table that T3 holds an X lock on Z, then show who holds what.

(output appears here)

التمرين 2

T2 عايزة X على X (والـ T1 ماسكة S). اكتب استعلام يعد الـ locks الموجودة على X ويقرر.

T2 wants X on X while T1 holds S. Write a query that counts the existing locks on X and decides.

(output appears here)

مشروع صغير في آخر المحاضرة

مدير locks صغير بجدول

الداتا: Lock requests: 5 requests from 3 transactions. Written for this course (our own data, free to reuse).

License / source: Our own sample data; SQLite is in the public domain. Dataset page

الخطوات

  1. جدول requests فيه الطلبات بالترتيب (item, tx, mode).
  2. جدول lock_table فاضي. ضيف الطلب الأول والتاني (S على A من T1 وS على A من T2) لأنهم متوافقين.
  3. الطلب التالت: T3 عايزة X على A. اكتب استعلام يقرر. المتوقع: wait.
  4. اعرض lock_table. المتوقع: صفين.
Expected output (press Run to produce your own):
t3_answer
---------
wait     
(1 row)
item | tx | mode
-----+----+-----
A    | T1 | S   
A    | T2 | S   
(2 rows)

Credits: the topic order follows the lecture list of the course TDDD12 Database Technology at Linköping University (https://www.ida.liu.se/~TDDD12/fo/), whose reading guide is based on the textbook Fundamentals of Database Systems by Elmasri and Navathe. We found no reuse licence on that page, so we link to it and copy nothing from it: the explanations, diagrams, data and code in this lecture are our own. Reference: Elmasri and Navathe, Fundamentals of Database Systems, the chapters on concurrency control.