Module 5
Module 5
Module 5
Transaction management,
Concurrency Control
2
3
What is a
Database
Transaction?
A Database
Transaction is a
logical unit of
processing in a
DBMS which
entails one or
more database
access operation.
4
Transaction in DBMS
Transaction management in
DBMS ensures that data is
restored consistently when
the computer restarts after a
crash. After a system crash,
restoring the data is essential
for data recovery. The
restoration uses checkpoints,
transaction logs, and crash
recovery methods.
Example of a Transaction in DBMS: 6
A simple example of a transaction will be dealing with the bank accounts of two
users let say A and B. A simple transaction of moving an amount of 5000 from A
to B engages many low-level jobs.
As the amount of Rs. 5000 gets transferred from the A's account to B's account, a
series of tasks gets performed in the background of the screen.
This very simple and small transaction includes several steps:
1.Reading the amount of A’s account. Denoted as R(A)
2.debit A's bank account by 5000 i.e., A-5000
3.writing the modified account details with write operation. i.e., W(A)
4.Reading the amount of B’s account i.e., R(B)
5.credit A's bank account by 5000 i.e., B+5000
6.writing the modified account details with write operation. i.e., W(B)
Why do you need 7
concurrency in Transactions?
A transaction can
Commit after completing its actions, or
Abort because of
Internal DBMS decision: restart
System crash: power, disk failure, …
Unexpected situation: unable to access disk, data
value, …
A transaction interrupted in the middle could leave
the database inconsistent
DBMS needs to remove the effects of partial
transactions to ensure atomicity: either all a
transaction’s actions are performed or none
14
Atomicity cont.
The general problem illustrated here is that Tl may write some value
into A but that is not yet committed because its transaction is still in
progress.T1 is interleaved ,Transaction T2 started executing and used
value of A. After its transaction is done, it is committed.
If T1 is aborted before it is committed because of some system or power
failure then T2 has read a value for A that should never have been there.
If T2 had not yet committed, we could deal with the situation by
cascading the abort of T1 and also aborting T2; this process
recursively aborts any transaction that read data written by T2, and so
on. But T2 has already committed, and so we cannot undo its actions
which is unrecoverable schedule.(as below)
28
If two transactions access the same object, and one wants to modify
it, their actions are effectively ordered serially· all actions of one of
these transactions (the one that gets the lock on the common object
first) are completed before (this lock is released and) the other
transaction can proceed.
38
Non-conflicting pairs
R(A),R(A)
R(B),R(A)
W(B),W(A)
R(B), W(A)
W(A), R(B)
Conflicting Pairs
• W(A), R(A)
R(A), W(A)
45
EXAMPLE1:
S2
S1
46
S1 and S2 are
conflict equivalent
but S3 schedule is
not conflict
equivalent to S1,S2
as the conflicting
operations order is
not same
S3
47
S S’
T1 T2 T1 T2 R(A)
R(A)
W(A)
W(A)
R(B)
R(A) R(A)
W(A) W(A)
R(B)
48
S
R(A)
T1 T2 T1 R(A)
T2
W(A) W(A)
R(B)
R(A)
R(B) R(A)
W(A)
W(A)
49
S
Conflict pairs no change in position
T1
R(A) T2
W(A)
R(A)
W(A)
R(B)
51
Conflict Serializability
Schedule is conflict serializable if it is conflict
equivalent to some serial schedule.
It is useful to capture all potential conflicts
between the transactions in a schedule in a
precedence graph, also called a serializability
graph.
The precedence graph for a schedule S
contains:
A node for each committed transaction in S.
An arc from Ti to Tj if an action of Ti precedes
and conflicts with one of Tj's actions.
52
EXAMPLE1
53
R(X)
T1 T2 T3
R(Y)
R(X)
R(Y)
R(Z)
W(Y)
W(Z)
R(Z)
W(X)
W(Z)
55 if
• A schedule S is conflict serializable if and only
its precedence graph is acyclic.
• An equivalent serial schedule in this case is given
by any topological sort over the precedence
graph.
T1 T2
Equivalent to
T2-T3-T1
SERIAL
SCHEDULE
T3
56
View Serializability
Conflict serializability is sufficient but not necessary for
serializability. A more general sufficient condition is view
serializability.
Two schedules S1 and S2 over the same set of transactions -
any transaction that appears in either S1 or S2 must also appear
in the other - are view equivalent under these conditions:
1. If Ti reads the initial value of object A in S1, it must also read
the initial value of A in S2.
2. If Ti reads a value of A written by Tj in S1, it must also read the
value of A written by Tj in S2.
3. For each data object A, the transaction (if any) that performs the
final write on A in S1 must also perform the final write on A in S2.
57
S S’
R(A)
T1 T2 T1 R(A)
T2
W(A) W(A)
R(B)
R(A) W(B)
W(A)
R(B)
R(A)
W(B)
W(A)
R(B) R(B)
W(B) W(B)
58
2PL
Strict 2PL ensures that the precedence graph
for any schedule that it allows is acyclic. A
widely studied variant of Strict 2PL, called Two-
Phase Locking (2PL), relaxes
the second rule of Strict 2PL to allow
transactions to release locks before the end,
that is, before the commit or abort action.
Thus, every transaction has a `growing' phase
in which it acquires locks, followed by a
`shrinking' phase in which it releases locks.
59