Final Chap5 Tran & Con
Final Chap5 Tran & Con
Final Chap5 Tran & Con
ACCOUNT A ACCOUNT B
transfer $500
XYZ PQR
$1000 $0
1. Debit A
2. Credit B
Concurrency Control
Net Result
ACCOUNT A ACCOUNT B ACCOUNT C
Ramesh Rajesh Amit
£500 £200 £500
Concurrency in Transaction Execution
Transaction Serializability
T1: T2:
read_item(x)
X:= X - N;
write_item(X);
read_item(Y);
time Y:=Y + N;
write_item(Y);
read_item(X);
X:= X + M;
write_item(X);
Example of Nonserial Schedules
Schedule A
T1: T2:
read_item(X);
X:= X - N;
read_item(X);
time
X:= X + M;
write_item(X);
read_item(Y);
write_item(X);
Y:=Y + N;
write_item(Y);
5.2 Methods for Serializability
• Protocols that, if followed by every transaction, will ensure
serializability of all schedules in which the transactions
participate. They may use locking techniques of data items
to prevent multiple transactions from accessing items
concurrently.
Partially
Committed
Committed
Active
Failed Aborted
5.3.1 Locking Techniques for
Concurrency Control
• The concept of locking data items is one of the main
techniques used for controlling the concurrent execution of
transactions.
read_lock(Y); read_lock(X);
read_lock(X);
read_item(X);
read_item(Y);
unlock(Y); write_lock(Y);
read_item(X);
unlock(X);
X:=X+Y;
read_item(Y);
write_lock(X);
write_item(X); Y:=X+Y;
write_item(Y);
unlock(Y);
unlock(X);
Locking Problems
• Deadlock: when each of two transactions is waiting for the
other to release an item.
transaction transaction
with smaller with larger
timestamp timestamp
T1 T2
T1 acquired exclusive
write_lock(X )
lock on X
write_lock(Y ) T2 acquired exclusive
t lock on Y
write_lock(Y ) i No one can finish,
m because both are in
write_lock(X ) the waiting state
e
Dead Lock (continued)
• In other words:
– A number of transactions (grater than one) hold lock on
one item and wait to acquire another
– No one of the waiting transactions can acquire locks on
all necessary items
Dead Lock (continued)
• Dead lock examples:
a)
– T1 has locked X and waits to lock Y
– T2 has locked Y and waits to lock Z
– T3 has locked Z and waits to lock X
b)
– BothT1 and T2 have acquired sharable locks on X and wait to
lock X exclusively
• All that results in a cyclic wait for graph
T2 waits for T1
T1 T2
T1 waits for T2
Dead Lock Prevention Techniques
• Problems:
– What if a transaction cannot predetermine all items it is
going to use? (e.g. a sequence of interactive SQL
statements comprising one database transaction)
– What if a database item that is already locked by another
transaction will be released very soon? (i.e. the transaction
is aborted in vane)
Using Timestamps
• Timestamps are used together with two phase locking
• DBMS assigns a timestamp TS to each transaction T entering the
system
• If Ti starts before Tj , then
TS (Ti ) < TS (Tj) (*Ti is older than Tj )
• WAIT-DIE protocol:
– An older transaction can wait on an item locked by a younger one,
– But a younger one gets killed (aborted and restarted with the
same timestamp) instead of waiting
– Since each transaction involved in a dead lock has a different age,
the dead lock will be prevented
Using Timestamps (continued)
• WOUND-WAIT protocol:
– If a transaction tries to lock an item held by another
one, the older transaction wound the younger one
(causes abort and restart with the same timestamp),
– But the younger one is allowed to wait
– So, the oldest transaction will be allowed to finish