Database users access shared data.
Integrity must be preserved.
Transaction - is an execution of a program
Transactions [308]
Data Entities A,B,C ... (pages, tuples, relations, etc)
Transactions built with
begin - creates private workspace
read(X) - look for X in workspace else on disk
write(X) - write X onto disk
end - end of transaction
But consider two executions of program INCREMENT:
begin read(A); A = A +1; write(A); end
| time | T1 | T2 | A in DB |
| 0 | begin | 7 | |
| 1 | begin | 7 | |
| 2 | read(A) | 7 | |
| 3 | read(A) | 7 | |
| 4 | A=A+1 | 7 | |
| 5 | A=A+1 | 7 | |
| 6 | write(A) | 8 | |
| 7 | write(A) | 8 | |
| 8 | end | 8 | |
| 9 | end | 8 |
Locks [309]
begin lock(A); read(A); A = A + 1; write(A); unlock(A); end
| time | T1 | T2 | A in DB |
| 0 | begin | 7 | |
| 1 | begin | 7 | |
| 2 | lock(A) | 7 | |
| 3 | lock(A) | 7 | |
| 4 | read(A) | 7 | |
| 5 | A=A+1 | 7 | |
| 6 | write(A) | 8 | |
| 7 | unlock(A) | 8 | |
| 8 | end | 8 | |
| 9 | read(A) | 8 | |
| 10 | A=A+1 | 8 | |
| 11 | write(A) | 9 | |
| 12 | unlock(A) | 9 | |
| 13 | end | 9 |
Schedule [310]
Schedule [311]
Assume A=50,B=40,C=10.
(a) is serial (final values are 40,30,30)
(b) is serializable (final values are 40,30,30)
(c) is not serializable (final values are 40,50,30)
| T1 | T2 |
| read(A) | |
| A=A-10 | |
| write(A) | |
| read(B) | |
| B=B+10 | |
| write(B) | |
| read(B) | |
| B=B-20 | |
| write(B) | |
| read(C) | |
| C=C+20 | |
| write(C) |
| T1 | T2 |
| read(A) | |
| read(B) | |
| A=A-10 | |
| B=B-20 | |
| write(A) | |
| write(B) | |
| read(B) | |
| read(C) | |
| B=B+10 | |
| C=C+20 | |
| write(B) | |
| write(C) |
| T1 | T2 |
| read(A) | |
| A=A-10 | |
| read(B) | |
| write(A) | |
| B=B-20 | |
| read(B) | |
| write(B) | |
| B=B+10 | |
| read(C) | |
| write(B) | |
| C=C+20 | |
| write(C) |
Test for Serializability [312]
Input: Schedule S of transactions with locks
Output: YES if S is serializable
Method:
| T1 | T2 | T3 |
| lock(A) | ||
| unlock(A) | ||
| lock(A) | ||
| unlock(A) | ||
| lock(B) | ||
| unlock(B) | ||
| lock(B) | ||
| unlock(B) |
| T1 | T2 |
| lock(A) | |
| unlock(A) | |
| lock(A) | |
| lock(B) | |
| unlock(A) | |
| unlock(B) | |
| lock(B) | |
| unlock(B) |
Serializable:
NOT serializable:
2-Phase Locking Protocol [313]
Example of 2-phase protocol:
lock(A); lock(B) A = A+1; B= B+3; unlock(B); unlock(A);
Example of not using 2-phase protocol
lock(A); A = A + 1; unlock(A); lock(B); B = B+3; unlock(B);
This is the same transaction as T2 in earlier example.
In the previous case, there was a serializable schedule but that does not guarantee that all of them would have been.
In the dynamic case, any schedule might result. If it follows 2-phase locking protocol, all schedules are serializable, hence equivalent to a serial schedule.
Share Locks [314]
Improved concurrency with
Any # of transactions can hold a share lock at any time
At most one transaction can hold an exclusive lock. No transaction can hold a share lock at the same time.
INDEX: INTENTIONAL EMPTY [315]
INDEX: INTENTIONAL EMPTY [316]