Unit 05 Transaction Management Dbms
Unit 05 Transaction Management Dbms
Concurrency Control
⮚ Transaction Management(2)-ACID properties, transactions, schedules and
concurrent execution of transactions,
1. read(A)
2. A := A + A*0.1
3. write(A)
5
Transaction Properties : A C I D
6
Transaction Properties : A C I D
In Transaction T2
A=? , B=?, A+B=?
Although multiple transactions may execute concurrently, each
transaction must be
unaware of other concurrently executing transactions.
6
Transaction Properties : A C I D
8
Transaction States
Advantage Issues
Concurrent Transaction Executions may destroy
the consistency of the database
✔increased processor and disk
utilization, leading to better transaction
throughput
E.g. one transaction can be using the CPU Solution
while another is reading from or writing to ⮚Serializability
the disk ⮚Concurrency control schemes
✔reduced average response time for ✔Mechanisms to achieve isolation.
transactions: short transactions need not ✔To control the interaction among the concurrent
wait behind long ones. transactions in order to prevent them from
destroying the consistency of the database
11
Serializability
⮚Schedules
⮚Serializability
⮚Types of
Serializability
⮚Serializability
Checking
12
Schedules
13
Schedules
14
Question : Identify the type of Given Schedule
15
Question : Check the consistency property of
given schedule [ A=1000, B=1000 ]
S1 S2
16
Question : Check the consistency property of
given schedule [ A=1000, B=1000 ]
S1 S2
17
Serializability
19
Conflict Serializability : Identify pairs of Conflict
Instructions
What is Conflicting
Instruction??
1. If I1, I2 are of different Transaction
2. If I1, I2 access same data object
3. If one of them is write instruction
20
Conflict Serializability : Check the Conflict
serializablity of given Schedule
Hint : If a schedule S(CS) can be transformed into a Serial schedule S´(SS) by a series of
swaps of non-conflicting instructions, we say that S and S´ are conflict equivalent. And S
is Conflict Serializable
21
Conflict Serializability : Check the Conflict
serializablity of given Schedule
It is non-conflict Serializable
22
Conflict Serializability : Check the Conflict
serializablity of given Schedule
23
Precedence Graph : Method to check Conflict
Serializability
A directed graph, called a precedence graph, from S. This graph consists of a pair G = (V,
E), where V is a set of vertices and E is a set of edges.
The set of vertices consists of all the transactions participating in the schedule.
The set of edges consists of all edges
Ti →Tj for which one of three conditions holds:
If an edge Ti → Tj exists in the precedence graph, then, in any serial schedule S equivalent
to S, Ti must appear before Tj .
If the precedence graph for S has a cycle, then schedule S is not conflict serializable. If the
graph contains no cycles, then the schedule S is conflict serializable.
24
Precedence Graph : Example
25
Precedence Graph : Check conflict
Serializability
26
View Serializability
Let S and S´ be two schedules with the same set of transactions.
S and S´ are view equivalent if the following three conditions are met, for each data item
Q,
1. If in schedule S, transaction Ti reads the initial value of Q, then in schedule
S’also transaction Ti must read the initial value of Q.
2. If in schedule S transaction Ti executes read(Q), and that value was produced
by transaction Tj (if any), then inschedule S’also transaction Ti must
read the value of Q that was produced by the same write(Q) operation of
transaction Tj .
3. The transaction (if any) that performs the final write(Q) operation in
schedule S must also perform the final write(Q) operation in schedule S’.
As can be seen, view equivalence is also based purely on reads and writes
alone.
Sequence of First Read, R-W and Final Write should be same in both Schedules29
View Serializability : Check the View of given
Schedule
Hint: Sequence of First Read, R-W and Final Write should be same in both Schedules
28
Recoverable Schedule
29
Cascading Rollback
30
Module 3- Transaction Management and
Concurrency Control
⮚ Transaction Management(2)-ACID properties, transactions, schedules and
concurrent execution of transactions,
32
Lock Compatibility
Lock-compatibility matrix
A transaction may be granted a lock on an item if the requested lock is compatible with
locks already held on the item by other transactions
Any number of transactions can hold shared locks on an item,
◦ But if any transaction holds an exclusive on the item no other transaction may hold
any lock on the item.
If a lock cannot be granted, the requesting transaction is made to wait till all
incompatible locks held by other transactions have been released. The lock is then
granted.
33
Lock-based Protocol : Convert Following in Lock-
Based Protocol
34
Pitfalls of Lock-Based Protocols
35
The Two-Phase Locking Protocol
36
Two Phase Locking Protocol : Convert
Following in Two Phase Locking Protocol
37
Strict Two Phase, Rigorous Two Phase
38
Convert Following in Strict Two Phase
Locking Protocol
41
Lock Table
⮚Dark blue rectangles indicate granted locks; light blue
indicate waiting requests
⮚Lock table also records the type of lock granted or
requested
⮚New request is added to the end of the queue of
requests for the data item, and granted if it is compatible
with all earlier locks
⮚Unlock requests result in the request being deleted,
and later requests are checked to see if they can now be
granted
⮚If transaction aborts, all waiting or granted requests of
the transaction are deleted
◦ lock manager may keep a list of locks held by each
transaction, to implement this efficiently
42
Module 3- Transaction Management and
Concurrency Control
⮚ Transaction Management(2)-ACID properties, transactions, schedules and
concurrent execution of transactions,
T1 T2
lock-X on X
write (X)
lock-X on Y
write (X)
wait for lock-X on X
44
Deadlock Handling
System is deadlocked if there is a set of transactions such that every transaction in the set
is waiting for another transaction in the set.
Deadlock prevention protocols ensure that the system will never enter into a deadlock
state.
wait-die scheme — non-preemptive
◦ Older transaction may wait for younger one to release data item. (older means smaller
timestamp) Younger transactions never wait for older ones; they are rolled back instead.
◦ A transaction may die several times before acquiring needed data item
45
Consider following Deadlock Scenario:
Apply Wait Die
T1 T2
1 lock-X on X
2 write (X)
3 lock-X on Y
4 write (X)
5 wait for lock-X on X T2 Will not wait for T1,
It Die itself [Rollback
T2]
6 wait for lock-X on Y
46
Consider following Deadlock Scenario:
Apply Wound Wait
T1 T2
1 lock-X on X
2 write (X)
3 lock-X on Y
4 write (X)
5 wait for lock-X on X
T1 Forces T2 to
Rollback.
47
Deadlock Detection
48
Module 3- Transaction Management and
Concurrency Control
⮚ Transaction Management(2)-ACID properties, transactions, schedules and
concurrent execution of transactions,
⮚Transaction failure :
Logical errors: transaction cannot complete due to some internal error condition
System errors: the database system must terminate an active transaction due to an
error condition (e.g., deadlock)
⮚System crash: a power failure or other hardware or software failure causes the
system to crash. It is assumed that non-volatile storage contents are not corrupted.
⮚ Disk failure: a head crash or similar failure destroys all or part of disk storage
50
Recovery Systems
52
Log-Based Recovery
⮚A log is a sequence of log records. The records keep information about update activities
on the database. The log is kept on stable storage
⮚When transaction Ti starts, it registers itself by writing a <Ti start> log record
⮚When Ti finishes it last statement, the log record <Ti commit> is written.
53
What is Log Record for Following
Transaction
display(A+B+C)
Commit
<Ti commit>
54
Immediate Database Modification
55
Immediate Database Modification : Recovery
✔redo(Ti) sets the value of all data items updated by Ti to the new values, going forward from the
first log record for Ti
✔Transaction Ti needs to be redone if the log contains both the record < Ti start> and the record<Ti
Commit> .
56
Immediate Database Modification : Recovery
Example : Guess the Undo, Redo Action
57
Deferred Database Modification
6
1
Deffered Database Modification : Recovery
Example : Guess the Undo, Redo Action
60
Checkpoints
Solution
Streamline recovery procedure by periodically performing checkpointing
1.Output all log records currently residing in main memory onto stable
storage.
2. Output all modified buffer blocks to the disk.
3. Write a log record <checkpoint> onto stable storage.
61
Checkpoints
⮚ During recovery we need to consider only the most recent transaction Ti that started before the
checkpoint, and transactions that started after Ti .
⮚ Scan backwards from end of log to find the most recent <checkpoint> record
⮚ Need only consider the part of log following above start record. Earlier part of log can be
ignored during recovery, and can be erased whenever desired.
⮚For all transactions (starting from Ti or later) with no <Ti commit>, or <Ti abort> found in log
execute undo(Ti ). (Done only in case of immediate modification.)
⮚ Scanning forward in the log, for all transactions starting from Ti or later with a <Ti commit>,
execute redo(Ti ).
62
Checkpoints Example : Consider the
scenario and suggest recovery
63
Transaction Management
Refer :
Abraham Silberschatz, Henry F. Korth and S. Sudarshan, Database System Concepts ,McGraw Hill
64
Thank
You! 67