0% found this document useful (0 votes)
2 views

Module 5

The document discusses transaction management in database systems, focusing on concurrency control mechanisms like locking protocols and timestamp ordering. It explains the importance of isolation, deadlock avoidance, and the two-phase locking protocol, detailing how transactions acquire and release locks. Additionally, it introduces the Thomas Write Rule as a method to enhance concurrency by allowing obsolete writes to be ignored under certain conditions.

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 5

The document discusses transaction management in database systems, focusing on concurrency control mechanisms like locking protocols and timestamp ordering. It explains the importance of isolation, deadlock avoidance, and the two-phase locking protocol, detailing how transactions acquire and release locks. Additionally, it introduces the Thomas Write Rule as a method to enhance concurrency by allowing obsolete writes to be ignored under certain conditions.

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 46

CSE2004 – DBMS

TRANSACTION
•Isolation property may no long be preserved.
•so we need Concurrency control scheme
lock is a mechanism to
control concurrent access
to a data item

Grants the lock to the


transaction
Data item can be locked in two modes
X-lock is requested using
lock-X instruction.

S-lock is requested using


lock-S instruction.
Lock –compatibility matrix

Compatible??
Ex: for performing locking
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
Consider the example for
deadlock:
Starvation : If a transaction T never
make a progress , then is said to be
starved.
When a transaction Ti request a lock
on a data item Q in a particular mode M
We can avoid starvation by:
•There is no othertransaction holding a lock
on Q in a mode that conflicts with M

•There is no other transaction that is waiting for a lock on Q


and that made its lock request before Ti
Set of rules each transaction in the
system follow this rule
•it is for ,when a transaction may
lock or unlock each of its data
items.
•It restricts the number possible
schedules
A schedule s is legal under a given locking protocol ,if s
possible schedule for a set of transaction that follows the
rules of the locking protocol
A locking protocol ensures conflict
serializability if and only if all legal
schedules are conflict serializable.
This is a protocol which ensures conflict
serializable schedules.

•Phase 1: Growing Phase


•transaction may obtain locks
•transaction may not release locks

• Phase 2: Shrinking Phase


• transaction may release locks
• transaction may not obtain locks
T1 T2 GROWING PHASE from in the given example
X(A) X(A)……………………R(B)
R(A)
W(A)
R(A) SHRINKING PHASE: from in the given example
S(B) U(A)………………U(B)
R(B)
…..
…… Once the transaction releases a lock ,it
….. enters the shrinking phase and it can
U(A)
….. issue no more lock requests.
…..
…. •R(A) in T2 has to wait until lock is
…..
U(B) released by T1 and then it is executed.
•Initially a transaction is in the
growing phase. The transaction
acquires locks as needed.

•Once the transaction releases a lock ,it enters


the shrinking phase and it can issue no more
lock requests.

•Unlock instruction do not need to appear at


the end of the transaction.
T1: lock-S(A);
read (A);
unlock(A); lock-
S(B);
read (B);
unlock(B);
display(A+B);

T1: lock-s(A);
read (A);
lock-S(B);
read (B);
unlock(A);
Unlock(B);
display(A+BLB)S;IT
The point in the schedule where
the transaction has obtained its final
lock (the end of growing phase)

•Two-phase locking does not ensure freedom


from deadlocks
•cascading rollback may occur under two phase
locking
•(a)Strict two phase locking (avoiding
cascading rollback )

•(b)rigorous two phase locking


all exclusive mode
locks of a transaction held
until the transaction
commit

all locks of a transaction held until


the transaction commit
Ayt
T1 X(B) W(B U(B)
) Example 1:
T2 S(A) R(A) U(A) X(B) W(B U(B) It is not in2PL bcoz after unlock no
) locks to be acquired.
But in the above example after A
T1 X(B) W(B U(B) dataitem is unlocked in transaction
) T1 later it acquires lock on B data
T2 S(A) X(B) R(A) W(B U(A) U(B) item
)
Example 2:
It is not in2PL bcoz X(B) is not
T1 X(B) W(B U(B) realesed in T2 but again X(B) in T1
) cannot acquire lock.
T2 S(A) R(A) U(A) X(B) W(B U(B)
) Example 3:
It is not in2PL bcoz in T2 unlock is
dome after lock is not possible.i.e
U(A)in T2 …..later T2 tries to
acquire lock on X(B) which is not
T1 X(B) W(B U(B) possible.
)
T2 S(A) R(A) X(B) U(A) W(B U(B)
Two-phase locking with lock conversions:
– First Phase:
•can acquire a lock-S on item
•can acquire a lock-X on item
•can convert a lock-S to a lock-X (upgrade)
– Second Phase:
•can release a lock-S
•can release a lock-X
•can convert a lock-X to a lock-S (downgrade)
•Another method for finding the serializability order

•The protocol manages concurrent execution such that the


time-stamps determine the serializability

•A time stamp is assigned by the database system before


the transaction starts execution.
Assume
transactions comes
in the order
T1.T2,T3

•If an old transaction Ti has time-stamp TS(Ti), a new


transaction Tj is assigned time-stamp TS(Tj) such that TS(Ti)
<TS(Tj).
BASIC TIME STAMP ORDERING
PROTOCOL
100 200 300
 Unique value is assigned to
T1 T2 T3
each transaction
Older Younger Youngest
 Tells the order (when they enter
into the system)
 Read-TS(RTS): Last (Latest)
transaction number which
T1 100 T2 200 T3 300 T1 100 T2 200 T3 300
performed READ operation
successfully. R(A) W(A)
 Write-TS(WTS) : Last (latest)
R(A) W(A)
transaction number which R(A) W(A)
performed WRITE operation
successfully.
 ALWAYS WHICH TRANSACTION
COMES FIRST WOULD
COMPLETE FIRST
To Know what is RTS and WTS

T1 T2 T3 T4
R(A)
W(A)
R(A)
R(A)
W(A)
For implementing this we use two simple methods

•Use the value of the system clock.

OR

•Use a logical counter.


The time-stamps determine the
serializability order , i.e. ensure
that Ti appears before Tj.
maintains for each data Q two timestamp values:
.

is the largest time-stamp of any transaction that executed write(Q)


successfully.

is the largest time-stamp of any transaction that executed read(Q)


successfully
•The timestamp ordering protocol ensures that any conflicting read
and write operations are executed in timestamp order

•The protocol operates as follows.


Suppose a transaction Ti issues a read(A) operation
a. If WTS(A) > TS(Ti),Rollback Ti.
b. Otherwise execute R(A) operation
c. Set RTS(A) = Max{RTS(A),TS(Ti)}
Suppose a transaction Ti issues a write(A) operation
a. If RTS(A)>TS(Ti) then Rollback Ti
b. If WTS(A)>TS(Ti) then Rollback Ti
c. Otherwise execute Write(A) Operation
Set WTS(A)=TS(Ti)
Conditions ------Conflict Pairs

 Rollback T1 T1 T2
T1 T2 T1 T2
W(A)
 Rollback T2 R(A) W(A)
W(A)
R(A)
 Rollback T2 W(A) . .
. .
. . .
. .
. . .
. .
. . .
. .
Commit .
. Commit .
. .
. . .
.****
. .****
EXAMPLE
T1 100 T2 200 T3 300 Suppose a transaction Ti issues a read(Q)
Older Younger Youngest operation
R(A) a. If WTS(A) > TS(Ti),Rollback Ti.
R(B) b. Otherwise execute R(A) operation
W(C)
c. Set RTS(A) = Max{RTS(A),TS(Ti)}
R(B)
R(C)
W(B)
W(A) Suppose a transaction Ti issues a write(Q)
operation
A B C a. If RTS(A)>TS(Ti) then Rollback Ti
RTS 0 0 0 b. If WTS(A)>TS(Ti) then Rollback Ti
WTS 0 0 0
c. Otherwise execute Write(A) Operation
A B C Set WTS(A)=TS(Ti)
RTS 100 300 100
WTS 300 0 100
•ensures •may not cascade-
freedom from free
•may not recoverable.
deadlock
• Problem with timestamp-ordering protocol:
• Suppose Ti aborts, but Tj has read a data item written by Ti , then
Tj must abort; if Tj had been allowed to commit earlier, the
schedule is not recoverable.
• Further, any transaction that has read a data item written by Tj
must abort this can lead to cascading rollback --- that is, a chain
of rollbacks
• Solution 1:
• A transaction is structured such that its writes are all performed at the
end of its processing
• All writes of a transaction form an atomic action; no transaction may
execute while a transaction is being written
• A transaction that aborts is restarted with a new timestamp
• Solution 2: Limited form of locking: wait for data to be committed before
reading it
• Solution 3: Use commit dependencies to ensure recoverability
Consider the example Example :

Don’t worry, We
have a soloution
THOMAS WRITE
RULE
• Modified version of the timestamp-ordering protocol in which obsolete
write operations may be ignored under certain circumstances.

• When Ti attempts to write data item Q, if TS(Ti) < W- timestamp(Q),


then Ti is attempting to write an obsolete value of {Q}.
– Rather than rolling back Ti as the timestamp ordering protocol would
have done, this {write} operation can be ignored.
• Otherwise this protocol is the same as the timestamp ordering protocol.
• Thomas' Write Rule allows greater potential concurrency.
– Allows some view-serializable schedules that are not conflict-serializable

You might also like