Ddbmsunit 2
Ddbmsunit 2
Management System
UNIT-2
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran khan U2.1
Concurrency Control
• Concurrency control is a method used to
ensure that database transactions are executed
in a safe manner (i.e., without data loss).
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.2 2
Transaction ACID rules
• A transaction = sequence of statements that
either all succeed, or all fail
D = durability
(completed transactions should remain
permanent)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.4 4
Distributed Concurrency Control
Objectives:
All concurrency control (CC) mechanisms must
ensure
• consistency of data items is preserved.
• Each atomic action completes in finite time
• Distribution of Processing power.
• Have lowest storage overheads
• Perform in network without communication
delays.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.5 5
Locks
The most common way in which access to items is
controlled is by “locks.” Lock manager is the part of
a DBMS that records, for each item I, whether one or
more transactions are reading or writing any part of I.
If so, the manager will forbid another transaction
from gaining access to I, provided the type of access
(read or write) could cause a conflict, such as the
duplicate selling of an airline seat.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.6 6
Example of locks
Lets consider two transaction T1 and T2. Each
accesses an item A, which we assume has an
integer value, and adds one to A.
Read A; A:=A+1;Write A;
-----------------------------------------------------------
T1: Read A A:=A+1 Write A
T2: Read A A:=A+1 Write A
-----------------------------------------------------------
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.7 7
Example of locks (cont…)
• The most common solution to this problem is to
provide a lock on A.
• Before reading A, a transaction T must lock A,
which prevents another transaction from accessing
A until T is finished with A.
• Furthermore, the need for T to set a lock on A
prevents T from accessing A if some other
transaction is already using A.
• T must wait until the other transaction unlocks A,
which it should do only after finishing with A .
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.8 8
Concurrent access with transaction
Agent A Agent B
… …
begin transaction
read account 754
($314.60)
… Begin transaction
wait
update account 754
($264.60) wait
…
commit wait
read account 754
($264.60)
…
update account 754
($214.60)
commit
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.9 9
Transaction Management with SQL
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1010
Two-Phase Locking Techniques
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1111
Two-Phase Locking Techniques
Cont…
Check these two condition before granting a lock on
transaction
1. There is no other transaction holding a lock on Q in a mode
that conflict with M.
2. There is no other transaction that is waiting for a lock on Q
and that made its lock request before Ti.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1212
Two-Phase Locking Techniques
Cont…
Locking (Growing) Phase: A transaction
applies locks (read or write) on desired data
items one at a time.
A transaction may obtain locks, but may not
release any lock.
Unlocking (Shrinking) Phase: A transaction
unlocks its locked data items one at a time.
A transaction may release locks, but may not
obtain any new locks.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1313
Putting Locks on Transaction
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1414
Types of 2PL
We Can employ one of the following 4 protocols
(based on 2PL) to ensure serializability for
DDBMSs…
Centralized 2PL: single site maintains all locking
information.
Primary Copy 2PL: For data copies, one copy
chosen as primary copy, others are slave
copies, Only need to write-lock primary copy
of data item that is to be updated. Changes can
be propagated to slaves
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1515
Types of 2PL Cont…
Distributed 2PL:
Lock managers distributed to every site. Each lock
manager responsible for locks for data at that site.
If data is not replicated, equivalent to primary copy
2PL. Otherwise, implements a Read-One-Write-All
(ROWA) replica control protocol.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1616
Types of 2PL Cont…
Majority Locking:
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1717
Deadlock
• A deadlock is a situation wherein two or
more competing actions are waiting for the
other to finish, and thus neither ever does
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1818
Deadlock Situation
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.1919
Necessary conditions
1. Mutual exclusion condition: a resource is either
assigned to one process or it is available
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.2222
Centralized approach for Deadlock Detection
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.2323
Distributed Deadlock Detection
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.2424
Distributed Deadlock Detection Cont…
The Chandy-Misra-Haas algorithm:
• Processes are allowed to request multiple
resources at once -- the growing phase of a
transaction can be speeded up.
• The consequence of this change is a process
may now wait on two or more resources at the
same time.
• When a process has to wait for some resources,
a problem message is generated and sent to the
process holding the resources.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.2525
Distributed Deadlock Detection Cont…
• The message consists of three numbers:
-> The process being blocked,
-> The process sending the message,
-> and the process receiving the message.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.2626
Distributed Deadlock Detection Cont…
• The message is then send to the process
holding the needed resources.
• If a message goes all the way around and
comes back to the original sender -- the
process that initiate the probe, a cycle
exists and the system is deadlocked.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.2727
Time stamp protocol
Time stamp
=
Assigning time to a transaction
Or
Assigning logical counter to any new
transaction .
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.2828
Time stamp protocol Cont…
Wait-Die and Wound-Wait
• Propose in the context of a distributed
system, but could be used in a centralized
system as well comes in two version
(Wait-die and Wound-wait) work as
follows:
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.2929
Time stamp protocol Cont…
• When transaction A requests a lock on
some item that is already locked by
transaction B, then
• Wait-Die: A waits if it is older than B;
otherwise, it “dies”- that is, A is rolled
back and restarted.
• Wound-Wait: A waits if it is younger
than B; otherwise, it “wounds” B – that is,
B is rolled back and restarted.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.3030
Wait-die
Wait-die
If (Ti older than Tj) then
Ti allowed to wait
Else
Abort Ti (rollback and restart later)
End if
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.3131
Wound-wait
Wound-wait
If (Ti older than Tj) then
Abort Tj (Ti wound Tj)
(rollback and restart later)
Else
Ti allowed to wait
End if
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.3232
No waiting algorithm
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.3333
Quorum based protocol
• A quorum is the minimum number of votes that a
distributed transaction has to obtain in order to be
allowed to perform an operation in a distributed
system.
• A quorum-based technique is implemented to
enforce consistent operation in a distributed
system.
• Quorum-based voting can be used as a replica
control method, as well as a commit method to
ensure transaction atomicity in the presence of
network partitioning.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.3434
Quorum based protocol Cont…
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.3535
Quorum based protocol Cont…
• Va + Vc > V, where 0 <= Va, Vc <= V.
• Before a transaction commits, it must obtain a
commit quorum Vc.
• Before a transaction aborts, it must obtain an abort
quorum Va.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.3737
Quorum based protocol Cont…
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.3838
Quorum based protocol Cont…
• Vr + Vw > V
• Vw > V/2
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.3939
Commit Protocols
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.4040
2 phase commit
Commit-request phase
1. The coordinator sends a query to commit
message to all cohorts.
2. The cohorts execute the transaction up to the
point where they will be asked to commit.
3. Each cohort replies with an agreement
message if the transaction succeeded, or an
abort message if the transaction failed.
4. The coordinator waits until it has a message
from each cohort.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.4141
Commit phase
• If the coordinator received an agreement
message from all cohorts during the commit-
request phase:
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.4343
Commit phase Cont…
• If any cohort sent an abort message during the
commit-request phase:
• The coordinator sends a rollback message to
all the cohorts.
• Each cohort undo the transaction using the
undo log, and releases the resources and
locks held during the transaction.
• Each cohort sends an acknowledgement to the
coordinator.
• The coordinator completes the transaction
when acknowledgements have been received.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.4444
Commit phase Cont…
Disadvantages
• The greatest disadvantage of the two-phase
commit protocol is the fact that it is a blocking
protocol. A node will block while it is waiting
for a message.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.4646
Non-blocking commitment protocol Cont…
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.4747
Coordinator
• The coordinator receives a transaction request.
If there is a failure at this point, the coordinator
aborts the transaction (i.e. upon recovery, it will
consider the transaction aborted). Otherwise,
the coordinator sends a start transaction
message to the cohorts and moves to the
waiting state.
Partitioned Networks
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.5151
Partitioned Networks Cont…
With modern communication networks, which are
capable of routing messages, the following
assumption about the network is also reasonable:
If site X cannot communicate with site Y but can
communicate with site Z, then site Z cannot
communicate with site Y either.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.5252
Partitioned Networks Cont…
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.5353
Check Points & Cold Starts/Restarts
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.5454
Check Points
Check Point- A previous consistent state is
marked by a check point. Check points are
very useful during the recovery procedure.
Performing a check point requires the
following operations:
• Writing to stable storage all log records and
all database updates which are still volatile
storage.
• Writing to the stable storage a check point
record.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.5555
Check Points Cont…
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.5656
Cold starts/restarts
• Cold restarts should be extremely rare in a data
base system, this is required if the log information
is lost at a site of the network.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.5757
Short Questions
• What are checkpoints. How it is differ from save
points.
• What is 2 phase locking technique?
• What is concurrency control in DDBMS?
• Explain quorum based protocol?
• What is distributed deadlock. How it occur in a
DDBMS.
• How locks can be applied in transactions.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.5858
Long Questions
• What is the difference between 2 phase and 3
phase locking technique.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63,By Imran Khan
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63 U2.6060