SlideShare a Scribd company logo
Advanced Database Systems andAdvanced Database Systems and
Data WarehousingData Warehousing
INTEGRITY AND CONCURRENCYINTEGRITY AND CONCURRENCY
IN DATABASE SYSTEMSIN DATABASE SYSTEMS
By: Benmammass MehdiBy: Benmammass Mehdi
OutlineOutline
► IntegrityIntegrity
 IntroductionIntroduction
 Achieving integrity in a database systemAchieving integrity in a database system
► Integrity Subsystem ComponentIntegrity Subsystem Component
► Integrity RulesIntegrity Rules
► ConcurrencyConcurrency
 IntroductionIntroduction
 Some important definitionsSome important definitions
 Lock-Based ProtocolsLock-Based Protocols
 Deadlock Avoidance in lock-based protocolsDeadlock Avoidance in lock-based protocols
 Locking GranularityLocking Granularity
 Optimistic Concurrency ControlOptimistic Concurrency Control
► ConclusionConclusion
Introduction - IntegrityIntroduction - Integrity
► The main features that a database system shouldThe main features that a database system should
exhibit are :exhibit are :
 AccuracyAccuracy
 CorrectnessCorrectness
 ValidityValidity
► An integrity constraint guards against accidentalAn integrity constraint guards against accidental
damage of database.damage of database.
► It ensures data consistency by allowing onlyIt ensures data consistency by allowing only
authorized changes in the database.authorized changes in the database.
► TheThe Integrity SubsystemIntegrity Subsystem is a component of theis a component of the
DBMS.DBMS.
OutlineOutline
► IntegrityIntegrity
 IntroductionIntroduction
 Achieving integrity in a database systemAchieving integrity in a database system
► Integrity Subsystem ComponentIntegrity Subsystem Component
► Integrity ConstraintsIntegrity Constraints
► ConcurrencyConcurrency
 IntroductionIntroduction
 Some important definitionsSome important definitions
 Lock-Based ProtocolsLock-Based Protocols
 Deadlock Avoidance in lock-based protocolsDeadlock Avoidance in lock-based protocols
 Locking GranularityLocking Granularity
 Optimistic Concurrency ControlOptimistic Concurrency Control
► ConclusionConclusion
Integrity subsystemIntegrity subsystem
►The role of an integrity subsystem is :The role of an integrity subsystem is :
 Monitoring transactions and detecting integrityMonitoring transactions and detecting integrity
violations.violations.
 Take appropriate actions given a violation.Take appropriate actions given a violation.
►The integrity subsystem is provided with aThe integrity subsystem is provided with a
set of rules that define the following :set of rules that define the following :
 the errors to check for;the errors to check for;
 when to check for these errors;when to check for these errors;
 what to do if an error occurs.what to do if an error occurs.
Integrity RulesIntegrity Rules
►Set of rules stored in the system dictionarySet of rules stored in the system dictionary
by Integrity Rule Compiler.by Integrity Rule Compiler.
►A new integrity rule, before being adopted,A new integrity rule, before being adopted,
must fulfill all the existing rules.must fulfill all the existing rules.
RULE#1 : AFTER UPDATING sales.quantity :RULE#1 : AFTER UPDATING sales.quantity :
sales.quantity > 0sales.quantity > 0
ELSEELSE
DO ;DO ;
set return code to “RULE#1 violated” ;set return code to “RULE#1 violated” ;
REJECT ;REJECT ;
END ;END ;
Integrity RulesIntegrity Rules
►The general structure of an integrity rule isThe general structure of an integrity rule is
 Trigger condition (after updating, inserting…)Trigger condition (after updating, inserting…)
 Constraint (sales.quantity >0)Constraint (sales.quantity >0)
 Violation response (else do…)Violation response (else do…)
►There are three types of integrity rulesThere are three types of integrity rules
 Domain Integrity RuleDomain Integrity Rule
 The relation integrity rulesThe relation integrity rules
 The fansets integrity constraintsThe fansets integrity constraints
Domain Integrity Rule (1)Domain Integrity Rule (1)
DCL S#DCL S# PRIMARY DOMAIN CHARACTER (5)PRIMARY DOMAIN CHARACTER (5)
SUBSTR (S#,1,1) = ‘S’SUBSTR (S#,1,1) = ‘S’
AND IS_NUMERIC (SUBSTR (S#,2,4))AND IS_NUMERIC (SUBSTR (S#,2,4))
ELSEELSE
DO;DO;
Set return code to “S# domain ruleSet return code to “S# domain rule
violated” ;violated” ;
REJECT ;REJECT ;
END ;END ;
► S# is a string of 5 characters. Te first character isS# is a string of 5 characters. Te first character is
an S and the last 4 characters are numeric.an S and the last 4 characters are numeric.
Domain Integrity Rule (2)Domain Integrity Rule (2)
►Composite domains : a domain DATE whichComposite domains : a domain DATE which
is composed of three domains DAY,is composed of three domains DAY,
MONTH and YEARMONTH and YEAR
►User-Written Procedures.User-Written Procedures.
►Interdomain Procedures : some conversionInterdomain Procedures : some conversion
rules (procedures) may help for example torules (procedures) may help for example to
compare two values from two distinctcompare two values from two distinct
domains (distance expressed in kms anddomains (distance expressed in kms and
miles).miles).
Relation Integrity Rule (1)Relation Integrity Rule (1)
► Immediate record state constraintsImmediate record state constraints
After updating or inserting sales.quantity, verify :After updating or inserting sales.quantity, verify :
sales.quantity > 0sales.quantity > 0
► Immediate record transition constraintsImmediate record transition constraints
New_date > sales.dateNew_date > sales.date
► Immediate set state/transition constraintsImmediate set state/transition constraints
 define a key uniqueness and enforcing non-null values ofdefine a key uniqueness and enforcing non-null values of
the key (Entity Integrity Rule)the key (Entity Integrity Rule)
 impose referential integrity (Foreign Key Integrity Rule)impose referential integrity (Foreign Key Integrity Rule)
Relation Integrity Rule (2)Relation Integrity Rule (2)
►Deferred record state constraintsDeferred record state constraints
►Deferred record transition constraintsDeferred record transition constraints
►Deferred set state constraintsDeferred set state constraints
Applied at the end of the transactionApplied at the end of the transaction
((WHEN COMMITINGWHEN COMMITING). We need this kind). We need this kind
of constraints because sometimes the setof constraints because sometimes the set
of updates in a transaction violatesof updates in a transaction violates
temporarily the rule.temporarily the rule.
►Deferred set transition constraintsDeferred set transition constraints
Other Integrity ConstraintsOther Integrity Constraints
► Fanset Integrity RulesFanset Integrity Rules
 Used in network databases. They prevent integrityUsed in network databases. They prevent integrity
violations by providing referential integrity.violations by providing referential integrity.
► Triggered proceduresTriggered procedures
 Integrity rules are special case of triggered procedures.Integrity rules are special case of triggered procedures.
 Are useful to carry out the following tasks :Are useful to carry out the following tasks :
► Prevent the user that deleting a client will delete all its sales.Prevent the user that deleting a client will delete all its sales.
► Access security.Access security.
► Performance measurement of the database.Performance measurement of the database.
► Controlling stored record (compressing and decompressingControlling stored record (compressing and decompressing
data when storing and retrieving data).data when storing and retrieving data).
► Exception reporting (expiry date for medicaments)Exception reporting (expiry date for medicaments)
OutlineOutline
► IntegrityIntegrity
 IntroductionIntroduction
 Achieving integrity in a database systemAchieving integrity in a database system
► Integrity Subsystem ComponentIntegrity Subsystem Component
► Integrity ConstraintsIntegrity Constraints
► ConcurrencyConcurrency
 IntroductionIntroduction
 Some important definitionsSome important definitions
 Lock-Based ProtocolsLock-Based Protocols
 Deadlock Avoidance in lock-based protocolsDeadlock Avoidance in lock-based protocols
 Locking GranularityLocking Granularity
 Optimistic Concurrency ControlOptimistic Concurrency Control
► ConclusionConclusion
Concurrency Control - IntroductionConcurrency Control - Introduction
► ContentionContention occurs when two or more users try tooccurs when two or more users try to
access simultaneously the same record.access simultaneously the same record.
► ConcurrencyConcurrency occurs when multiple users haveoccurs when multiple users have
the ability to access the same resource and eachthe ability to access the same resource and each
user has access to the resource in isolation.user has access to the resource in isolation.
Concurrency is high when there is no apparentConcurrency is high when there is no apparent
wait time for a user to get its request. Concurrencywait time for a user to get its request. Concurrency
is low when wait times are evidentis low when wait times are evident
► ConsistencyConsistency occurs when users access aoccurs when users access a
shared resource and the resource exhibits theshared resource and the resource exhibits the
same characteristics and satisfies all thesame characteristics and satisfies all the
constraints among all operations.constraints among all operations.
Concurrency Control - IntroductionConcurrency Control - Introduction
► Example (Bank transactions) :Example (Bank transactions) :
 2 accounts A and B (assume balances A and2 accounts A and B (assume balances A and
B=100DH)B=100DH)
 2 transactions T1 and T2 that will be executed2 transactions T1 and T2 that will be executed
concurrentlyconcurrently
► T1T1 : start, A=A+100, B=B-100, COMMIT: start, A=A+100, B=B-100, COMMIT
► T2T2 : start, A=A*1.05, B=B*1.05, COMMIT: start, A=A*1.05, B=B*1.05, COMMIT
Concurrency Control - IntroductionConcurrency Control - Introduction
►T1T1
A=A+100
B=B-100B=B-100
COMMITCOMMIT
►T2T2
A=A*1.05A=A*1.05
B=B*1.05B=B*1.05
COMMITCOMMIT
►T1T1
A+100A+100
►T2T2
A=A*1.05A=A*1.05
B=B*1.05B=B*1.05
COMMITCOMMIT
►T1T1
B=B-100B=B-100
COMMITCOMMIT
A = 210 DHA = 210 DH
B = 0 DHB = 0 DH
A = 210 DHA = 210 DH
B = 5 DHB = 5 DH
Consider these two different sequences ofConsider these two different sequences of
execution :execution :
INTERFERENCINTERFERENC
EE
Concurrency Control - SolutionsConcurrency Control - Solutions
►Lock-Based ProtocolsLock-Based Protocols
►Timestamp TechniquesTimestamp Techniques
►Optimistic Concurrency ControlOptimistic Concurrency Control
Concurrency Control – LockConcurrency Control – Lock
ManagerManager► AA Lock managerLock manager can be implemented as a separatecan be implemented as a separate
process to which transactions send lock and unlockprocess to which transactions send lock and unlock
requestsrequests
► The lock manager replies to a lock request by sendingThe lock manager replies to a lock request by sending
a lock grant messages (or a message asking thea lock grant messages (or a message asking the
transaction to roll back, in case of a deadlock)transaction to roll back, in case of a deadlock)
► The requesting transaction waits until its request isThe requesting transaction waits until its request is
answeredanswered
► The lock manager maintains a data structure called aThe lock manager maintains a data structure called a
lock tablelock table to record granted locks and pendingto record granted locks and pending
requestsrequests
Concurrency – LB Protocols (1)Concurrency – LB Protocols (1)
Principle :Principle :
► Transactions ask for a lock on a record beforeTransactions ask for a lock on a record before
updating it.updating it.
► After update, the record is unlockedAfter update, the record is unlocked
► We have two types of locks :We have two types of locks :
 exclusive (X) modeexclusive (X) mode: data can be both read as well as: data can be both read as well as
written. X-lock is requested usingwritten. X-lock is requested using lock-Xlock-X instruction.instruction.
Records are the unit of locking.Records are the unit of locking.
 shared (S) modeshared (S) mode: data can only be read. S-lock is: data can only be read. S-lock is
requested usingrequested using lock-Slock-S instruction. Tables are the unitinstruction. Tables are the unit
of locking.of locking.
LB Protocols (2)LB Protocols (2)
Transaction A
Shared Lock
Exclusive Lock
Exclusive Lock
Accounts Table
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Transaction B
Shared Lock
Exclusive Lock
Exclusive Lock
Shared and Exclusive LocksShared and Exclusive Locks
LB Protocols (3)LB Protocols (3)
► Compatibility MatrixCompatibility Matrix
Shared lock Exclusive lock
Shared lock True False
Exclusive lock False False
Lock TableLock Table
► Black rectangles indicate grantedBlack rectangles indicate granted
locks, white ones indicate waitinglocks, white ones indicate waiting
requestsrequests
► Lock table also records the type ofLock table also records the type of
lock granted or requestedlock granted or requested
► New request is added to the endNew request is added to the end
of the queue of requests for theof the queue of requests for the
data item, and granted if it isdata item, and granted if it is
compatible with all earlier lockscompatible with all earlier locks
► Unlock requests result in theUnlock requests result in the
request being deleted, and laterrequest being deleted, and later
requests are checked to see ifrequests are checked to see if
they can now be grantedthey can now be granted
► If transaction aborts, all waiting orIf transaction aborts, all waiting or
granted requests of thegranted requests of the
transaction are deletedtransaction are deleted
 lock manager may keep a listlock manager may keep a list
of locks held by eachof locks held by each
transaction, to implement thistransaction, to implement this
efficientlyefficiently
LB Protocols (4) – PX ProtocolLB Protocols (4) – PX Protocol
►Any transaction that intends to update aAny transaction that intends to update a
record must first execute an exclusive lockrecord must first execute an exclusive lock
request (X-lock) on that record.request (X-lock) on that record.
►If the lock cannot be acquired, theIf the lock cannot be acquired, the
transaction goes into a wait state.transaction goes into a wait state.
►When the record becomes available, theWhen the record becomes available, the
lock can be granted and the transaction canlock can be granted and the transaction can
resume processing.resume processing.
LB Protocols (5) – PX ProtocolLB Protocols (5) – PX Protocol
► Example : 2 transactionsExample : 2 transactions
Transaction 1:Transaction 1: Transaction 2 :Transaction 2 :
lock-X(B)lock-X(B) lock-lock-
S(A)S(A)
read(B)read(B) read(A)read(A)
B = B -50B = B -50 unlock(A)unlock(A)
write(B)write(B) lock-S(B)lock-S(B)
unlock(B)unlock(B) read(B)read(B)
lock-X(A)lock-X(A) unlock(B)unlock(B)
read(A)read(A) display(A+B)display(A+B)
A = A + 50A = A + 50
write(A)write(A)
unlock(A)unlock(A)
LB Protocols (6) – PX ProtocolLB Protocols (6) – PX Protocol
Execution sequence :Execution sequence :
Transaction 1Transaction 1 Transaction 2Transaction 2 Concurrency control managerConcurrency control manager
lock-X(B)lock-X(B) grant-X(B)grant-X(B)
read(B)read(B)
B = B -50B = B -50
write(B)write(B)
unlock(B)unlock(B)
lock-S(A)lock-S(A) grant-S(A)grant-S(A)
read(A)read(A)
unlock(A)unlock(A)
lock-S(B)lock-S(B) grant-S(B)grant-S(B)
read(B)read(B)
unlock(B)unlock(B)
display(A+B)display(A+B)
lock-X(A)lock-X(A) grant-X(A)grant-X(A)
read(A)read(A)
A = A + 50A = A + 50
write(A)write(A)
unlock(A)unlock(A)
LB Protocols (7) – PX ProtocolLB Protocols (7) – PX Protocol
► Serializability :Serializability : interleaved execution sequence ofinterleaved execution sequence of
a set of transactions that will obtain the samea set of transactions that will obtain the same
results as if the transactions are processedresults as if the transactions are processed
serially.serially.
► We have to look at the lock requests of eachWe have to look at the lock requests of each
transaction and to find an order to execute themtransaction and to find an order to execute them
without any interference between then. Thewithout any interference between then. The
resulting sequence, if there is one, implies that theresulting sequence, if there is one, implies that the
two transactions are serializable.two transactions are serializable.
► PX Protocol then can be applied.PX Protocol then can be applied.
LB Protocols (7) – PX ProtocolLB Protocols (7) – PX Protocol
► Using the lock-based mechanism, deadlock andUsing the lock-based mechanism, deadlock and
starvation can occur. This is an example ofstarvation can occur. This is an example of
deadlock :deadlock :
Transaction A
Shared Lock
Already X-
Locked
Asks for an X-
Lock
Accounts
Table
Row 1
Row 2
Row 3
Row 4
Row 5
Row 6
Row 7
Row 8
Transaction B
Shared Lock
Asks for an X-
Lock
Already X-
Locked
LB Protocols – PXC ProtocolLB Protocols – PXC Protocol
►Derived from PX protocol.Derived from PX protocol.
►Exclusive locks are retained until end ofExclusive locks are retained until end of
transaction (COMMIT or ROLLBACK).transaction (COMMIT or ROLLBACK).
►PXC helps to avoid loss of updates bcausePXC helps to avoid loss of updates bcause
of ROLLBACK.of ROLLBACK.
 No transaction is allowed to update anNo transaction is allowed to update an
uncommitted changed record.uncommitted changed record.
LB Protocols – PS / PSCLB Protocols – PS / PSC
ProtocolsProtocols
► Any transaction that updates a record must firstlyAny transaction that updates a record must firstly
ask for a shared lock of that record.ask for a shared lock of that record.
► During the transaction, just before the updateDuring the transaction, just before the update
command, comes a request of changing the lock-Scommand, comes a request of changing the lock-S
to lock-X.to lock-X.
► A transaction should not be allowed to lock itselfA transaction should not be allowed to lock itself
out.out.
► The goal here is to limit the duration of X-locks.The goal here is to limit the duration of X-locks.
LB Protocols – PS ProtocolLB Protocols – PS Protocol
►Example (here deadlock occurs at T4):Example (here deadlock occurs at T4):
 Transaction ATransaction A Transaction BTransaction B
SFIND Record1SFIND Record1 ----
---- ----
---- SFIND R1SFIND R1
UPD Record1 --
---- ----
---- UPD R1UPD R1
TimeTime
T1T1
T2T2
T3T3
T4T4
LB Protocols – PU / PUCLB Protocols – PU / PUC
ProtocolProtocol►This protocol uses a third lock state : theThis protocol uses a third lock state : the
update lockupdate lock..
►Any transaction that intends to update aAny transaction that intends to update a
record is required to ask for U-lock of thatrecord is required to ask for U-lock of that
record. A U-lock is compatible with an S-record. A U-lock is compatible with an S-
lock but not with another U-lock.lock but not with another U-lock.
►Replacing S-locks by U-locks will preventReplacing S-locks by U-locks will prevent
deadlock.deadlock.
LB Protocols – PU / PUCLB Protocols – PU / PUC
ProtocolProtocol
► Compatibility matrix :Compatibility matrix :
X S U
X False False False
S False True True
U False True False
►ExampleExample : compare PU and PS protocols: compare PU and PS protocols
LB Protocols – PU ProtocolLB Protocols – PU Protocol
►This protocol is more efficient than theThis protocol is more efficient than the
previous ones.previous ones.
►It limits considerably deadlock, because itIt limits considerably deadlock, because it
decreases the number of S-locks.decreases the number of S-locks.
LB Protocols –Two Phase LockingLB Protocols –Two Phase Locking
ProtocolProtocol
► 2PL2PL ensures conflict-serializable schedules.ensures conflict-serializable schedules.
► 2PL includes two phases :2PL includes two phases :
 Growing phaseGrowing phase : transaction may obtain locks and: transaction may obtain locks and
may not release locksmay not release locks
 Shrinking phaseShrinking phase : transaction may release locks and: transaction may release locks and
may not obtain locks.may not obtain locks.
► The schedule is determined in the relation toThe schedule is determined in the relation to thethe
order of theirorder of their lock pointslock points..
► If all transactions are two-phase, then allIf all transactions are two-phase, then all
executions are serializable.executions are serializable.
LB Protocols –2PL ProtocolLB Protocols –2PL Protocol
Example :Example :
LB Protocols –2PL ProtocolLB Protocols –2PL Protocol
► There are many protocols derived from 2PL :There are many protocols derived from 2PL :
 Strict two-phase lockingStrict two-phase locking . Here a transaction must. Here a transaction must
hold all its exclusive locks till it commits.hold all its exclusive locks till it commits.
 Rigorous two-phase lockingRigorous two-phase locking is even stricter: hereis even stricter: here
allall locks (shared and exclusive) are held till commit. Inlocks (shared and exclusive) are held till commit. In
this protocol transactions can be serialized in the orderthis protocol transactions can be serialized in the order
in which they commit.in which they commit.
 Graph-based protocol :Graph-based protocol : we fix an order of accessingwe fix an order of accessing
data. If a transaction has to update Row2 and readdata. If a transaction has to update Row2 and read
Row1, it has to access these data in a predefined order.Row1, it has to access these data in a predefined order.
LB Protocols – Deadlock AvoidanceLB Protocols – Deadlock Avoidance
►Deadlock preventionDeadlock prevention protocols ensure thatprotocols ensure that
the system will never enter into a deadlockthe system will never enter into a deadlock
state. It can be achieved using differentstate. It can be achieved using different
strategies :strategies :
 Transaction SchedulingTransaction Scheduling
 Request RejectionRequest Rejection
 Transaction RetryTransaction Retry
LB-Protocols : Deadlock PreventionLB-Protocols : Deadlock Prevention
StrategiesStrategies
►Timeout-Based SchemesTimeout-Based Schemes ::
 a transaction waits for a lock only for a specifieda transaction waits for a lock only for a specified
amount of time. After that, the wait times outamount of time. After that, the wait times out
and the transaction is rolled back.and the transaction is rolled back.
 thus deadlocks are not possiblethus deadlocks are not possible
 simple to implement; but starvation is possible.simple to implement; but starvation is possible.
Also difficult to determine good value of theAlso difficult to determine good value of the
timeout interval.timeout interval.
LB-Protocols : Deadlock PreventionLB-Protocols : Deadlock Prevention
StrategiesStrategies
► What to do when a deadlock is detected ?What to do when a deadlock is detected ?
► Some transactions will have to roll back to breakSome transactions will have to roll back to break
deadlock. Select that transaction as victim thatdeadlock. Select that transaction as victim that
will incur minimum cost.will incur minimum cost.
► We have to determine how far to roll back theWe have to determine how far to roll back the
transaction. We can either carry out :transaction. We can either carry out :
 Total rollback: Abort the transaction and then restart it.Total rollback: Abort the transaction and then restart it.
 Partial rollback: it is more effective to roll backPartial rollback: it is more effective to roll back
transaction only as far as necessary to break deadlocktransaction only as far as necessary to break deadlock
Deadlock Avoidance : Transaction SchedulingDeadlock Avoidance : Transaction Scheduling
►Two transactions will not be runTwo transactions will not be run
concurrently if their data requirementsconcurrently if their data requirements
conflict.conflict.
►WeWe mustmust know what are the dataknow what are the data
requirements of each transaction before runrequirements of each transaction before run
time => impossible till runtime.time => impossible till runtime.
►Consequently, the lock unit is a set ofConsequently, the lock unit is a set of
records and locks are applied at transactionrecords and locks are applied at transaction
initiation instead of during execution.initiation instead of during execution.
Deadlock Avoidance : Request RejectionDeadlock Avoidance : Request Rejection
►The system rejects any lock request thatThe system rejects any lock request that
cannot be applied.cannot be applied.
►It uses theIt uses the deadlock detection algorithmdeadlock detection algorithm..
►When trying to grant a lock request, if aWhen trying to grant a lock request, if a
deadlock is detected, the transaction isdeadlock is detected, the transaction is
rejected.rejected.
Deadlock Avoidance : Transaction RetryDeadlock Avoidance : Transaction Retry
► Transactions are timestamped with their start time.Transactions are timestamped with their start time.
► Example : A requests a lock on a record already locked by BExample : A requests a lock on a record already locked by B
► wait-diewait-die scheme — non-preemptivescheme — non-preemptive
 A waits if it is older than B, otherwise, itA waits if it is older than B, otherwise, it diesdies and it is rolled back andand it is rolled back and
automatically retried.automatically retried.
 a transaction may die several times before acquiring its needed dataa transaction may die several times before acquiring its needed data
itemitem
► wound-waitwound-wait scheme — preemptivescheme — preemptive
 A waits if it is younger than B, otherwise itA waits if it is younger than B, otherwise it woundswounds (forces rollback) of(forces rollback) of
younger transaction instead of waiting for it.younger transaction instead of waiting for it.
 Younger transactions mayYounger transactions may waitwait for older ones.for older ones.
 Less rollbacks thanLess rollbacks than wait-diewait-die scheme.scheme.
► Transactions retain their timestamps even if they are rolledTransactions retain their timestamps even if they are rolled
back.back.
LB-Protocols : Deadlock Detection AlgorithmLB-Protocols : Deadlock Detection Algorithm
►The system is in a deadlock state if and onlyThe system is in a deadlock state if and only
ifif the wait-for graph has a cycle.the wait-for graph has a cycle.
►The system must invoke a deadlock-The system must invoke a deadlock-
detection algorithmdetection algorithm periodicallyperiodically to look forto look for
cycles.cycles.
LB-Protocols : Deadlock Detection AlgorithmLB-Protocols : Deadlock Detection Algorithm
Wait-for graph without a cycle Wait-for graph with a cycle
BackBack
LB Protocols : LockingLB Protocols : Locking
GranularityGranularity
► Allow data items to be of various sizes and defineAllow data items to be of various sizes and define
a hierarchy of data granularities, where the smalla hierarchy of data granularities, where the small
granularities are nested within larger ones.granularities are nested within larger ones.
► Can be represented graphically as a tree. When aCan be represented graphically as a tree. When a
transaction locks a node in the treetransaction locks a node in the tree explicitlyexplicitly, it, it
implicitlyimplicitly locks all the node's descendents in thelocks all the node's descendents in the
same mode.same mode.
► Granularity of lockingGranularity of locking (level in tree where locking(level in tree where locking
is done):is done):
 fine granularityfine granularity (lower in tree): high concurrency, high(lower in tree): high concurrency, high
locking overheadlocking overhead
 coarse granularitycoarse granularity (higher in tree): low locking(higher in tree): low locking
overhead, low concurrencyoverhead, low concurrency
LB Protocols : LockingLB Protocols : Locking
GranularityGranularity
►The highest level in the example hierarchy is theThe highest level in the example hierarchy is the
entire database.entire database.
►The levels below are of typeThe levels below are of type areaarea,, filefile andand recordrecord
in that order.in that order.
LB Protocols : Intent Locking ProtocolLB Protocols : Intent Locking Protocol
► In addition to S and X lock modes, there are threeIn addition to S and X lock modes, there are three
additional lock modes with multiple granularity:additional lock modes with multiple granularity:
 intention-sharedintention-shared (IS): indicates explicit locking at a(IS): indicates explicit locking at a
lower level of the tree but only with shared locks.lower level of the tree but only with shared locks.
 intentionintention--exclusiveexclusive (IX): indicates explicit locking at a(IX): indicates explicit locking at a
lower level with exclusive or shared lockslower level with exclusive or shared locks
 shared and intentionshared and intention--exclusiveexclusive (SIX): the subtree(SIX): the subtree
rooted by that node is locked explicitly in shared moderooted by that node is locked explicitly in shared mode
and explicit locking is being done at a lower level withand explicit locking is being done at a lower level with
exclusive-mode locks.exclusive-mode locks.
► Intention locks allow a higher level node to beIntention locks allow a higher level node to be
locked in S or X mode without having to check alllocked in S or X mode without having to check all
descendent nodes.descendent nodes.
LB Protocols : Intent Locking ProtocolLB Protocols : Intent Locking Protocol
IS IX S S IX X
IS
IX
S
S IX
X




×
  

×
×
× × × ×
×× ×
× ×
×
×
××
Compatibility Matrix :Compatibility Matrix :
LB Protocols : Intent Locking ProtocolLB Protocols : Intent Locking Protocol
► TransactionTransaction TTii can lock a nodecan lock a node QQ, using the following rules:, using the following rules:
1. The lock compatibility matrix must be observed.1. The lock compatibility matrix must be observed.
2. The root of the tree must be locked first, and may be locked in any2. The root of the tree must be locked first, and may be locked in any
mode.mode.
3. A node3. A node QQ can be locked bycan be locked by TTii in S or IS mode only if the parentin S or IS mode only if the parent
ofof QQ is currently locked byis currently locked by TTii in either IX or IS mode.in either IX or IS mode.
4. A node4. A node QQ can be locked bycan be locked by TTii in X, SIX, or IX mode only if thein X, SIX, or IX mode only if the
parent ofparent of QQ is currently locked byis currently locked by TTii in either IX or SIX mode.in either IX or SIX mode.
5.5. TTii can lock a node only if it has not previously unlocked any nodecan lock a node only if it has not previously unlocked any node
(that is,(that is, TTii is two-phase).is two-phase).
6.6. TTii can unlock a nodecan unlock a node QQ only if none of the children ofonly if none of the children of QQ are currentlyare currently
locked bylocked by TTii..
► Locks are acquired in root-to-leaf order, whereas they are released inLocks are acquired in root-to-leaf order, whereas they are released in
leaf-to-root order.leaf-to-root order.
LB ProtocolsLB Protocols
►Default Locking Behavior for OracleDefault Locking Behavior for Oracle
 A pure SELECT will not lock any row.A pure SELECT will not lock any row.
 INSERT, UPDATE or DELETE will place a rowINSERT, UPDATE or DELETE will place a row
Exclusive LockExclusive Lock (X-lock).(X-lock).
 SELECT...FROM...FOR UPDATE will place aSELECT...FROM...FOR UPDATE will place a
row Shared Lock (S-lock).row Shared Lock (S-lock).
LB ProtocolsLB Protocols
► Oracle Syntax:Oracle Syntax:
LOCK TABLE [LOCK TABLE [schema.schema.] table [] table [optionsoptions] IN] IN lock modelock mode MODE [NOWAIT]MODE [NOWAIT]
 Options:Options:
► PARTITION partitionPARTITION partition
► SUBPARTITION subpartitionSUBPARTITION subpartition
► @dblink@dblink
 Lock modes:Lock modes:
► EXCLUSIVEEXCLUSIVE
► SHARESHARE
► ROW EXCLUSIVEROW EXCLUSIVE
► SHARE ROW EXCLUSIVESHARE ROW EXCLUSIVE
► ROW SHARE* | SHARE UPDATE*ROW SHARE* | SHARE UPDATE*
Optimistic Concurrency ControlOptimistic Concurrency Control
ReadRead ValidationValidation WriteWrite
► A transaction in OCC is composed of three phases :A transaction in OCC is composed of three phases :
 Read PhaseRead Phase
Transactions access the database to load data,Transactions access the database to load data,
then they update data in a separate buffer.then they update data in a separate buffer.
 Validation phaseValidation phase
For each transaction, the system checks if there isFor each transaction, the system checks if there is
any conflict with another transaction. If there is, the transactionany conflict with another transaction. If there is, the transaction
is rolled back, otherwise the write phase can proceed.is rolled back, otherwise the write phase can proceed.
 Write phaseWrite phase
Updates are written from the buffer to the database.Updates are written from the buffer to the database.
CONCLUSIONCONCLUSION
►Locking is a pessimist concurrency control,Locking is a pessimist concurrency control,
because it assumes maximum contention.because it assumes maximum contention.
►OCC is dead-lock free because it does notOCC is dead-lock free because it does not
implement locking.implement locking.
ReferencesReferences
► Date BookDate Book
► Cooperative Cataloging in a Scalable Digital LibraryCooperative Cataloging in a Scalable Digital Library
SystemSystem, Dr Hachim Haddouti, Dr Hachim Haddouti
► Transaction ManagementTransaction Management, IBM Research Laboratory, San, IBM Research Laboratory, San
Jose CaliforniaJose California
► Performance ofPerformance of ConcurrencyConcurrency ControlControl Mechanisms inMechanisms in
Centralized Database SystemsCentralized Database Systems, Vijay Kumar., Vijay Kumar.
► Overview ofOverview of concurrencyconcurrency controlcontrol and locking forand locking for
databasesdatabases.. www.odbmsfacts.comwww.odbmsfacts.com/articles//articles/ concurrency_control_and_locking.htmlconcurrency_control_and_locking.html
Q&AQ&A
Integrity and ConcurrencyIntegrity and Concurrency
Control in Database SystemControl in Database System

More Related Content

Similar to integrity and cuncurrency in DBMS

Software archiecture lecture05
Software archiecture   lecture05Software archiecture   lecture05
Software archiecture lecture05
Luktalja
 

Similar to integrity and cuncurrency in DBMS (20)

Stormwatch micration
Stormwatch micrationStormwatch micration
Stormwatch micration
 
ds7_con.ppt
ds7_con.pptds7_con.ppt
ds7_con.ppt
 
Database security copy
Database security   copyDatabase security   copy
Database security copy
 
Concurrent transactions
Concurrent transactionsConcurrent transactions
Concurrent transactions
 
Cm5 secure code_training_1day_system configuration
Cm5 secure code_training_1day_system configurationCm5 secure code_training_1day_system configuration
Cm5 secure code_training_1day_system configuration
 
Database Testing
Database TestingDatabase Testing
Database Testing
 
LF_OVS_17_OvS-CD: Optimizing Flow Classification for OvS using the DPDK Membe...
LF_OVS_17_OvS-CD: Optimizing Flow Classification for OvS using the DPDK Membe...LF_OVS_17_OvS-CD: Optimizing Flow Classification for OvS using the DPDK Membe...
LF_OVS_17_OvS-CD: Optimizing Flow Classification for OvS using the DPDK Membe...
 
Automating the manual - feedback on including existing systems in configurati...
Automating the manual - feedback on including existing systems in configurati...Automating the manual - feedback on including existing systems in configurati...
Automating the manual - feedback on including existing systems in configurati...
 
Performance testing and rpt
Performance testing and rptPerformance testing and rpt
Performance testing and rpt
 
Dev confus.2020 compliance operator
Dev confus.2020 compliance operatorDev confus.2020 compliance operator
Dev confus.2020 compliance operator
 
Improve Security Visibility with AlienVault USM Correlation Directives
Improve Security Visibility with AlienVault USM Correlation DirectivesImprove Security Visibility with AlienVault USM Correlation Directives
Improve Security Visibility with AlienVault USM Correlation Directives
 
Design For Testability
Design For TestabilityDesign For Testability
Design For Testability
 
Software archiecture lecture05
Software archiecture   lecture05Software archiecture   lecture05
Software archiecture lecture05
 
Lost with data consistency
Lost with data consistencyLost with data consistency
Lost with data consistency
 
Self-adaptive container monitoring with performance-aware Load-Shedding policies
Self-adaptive container monitoring with performance-aware Load-Shedding policiesSelf-adaptive container monitoring with performance-aware Load-Shedding policies
Self-adaptive container monitoring with performance-aware Load-Shedding policies
 
Production Readiness Strategies in an Automated World
Production Readiness Strategies in an Automated WorldProduction Readiness Strategies in an Automated World
Production Readiness Strategies in an Automated World
 
The Linux Audit Framework
The Linux Audit FrameworkThe Linux Audit Framework
The Linux Audit Framework
 
Fault Tolerant Software Systems Using Software Configurations For Cloud Compu...
Fault Tolerant Software Systems Using Software Configurations For Cloud Compu...Fault Tolerant Software Systems Using Software Configurations For Cloud Compu...
Fault Tolerant Software Systems Using Software Configurations For Cloud Compu...
 
Os rtos.ppt
Os rtos.pptOs rtos.ppt
Os rtos.ppt
 
Chapter 4 u
Chapter 4 uChapter 4 u
Chapter 4 u
 

Recently uploaded

Exploratory Data Analysis - Dilip S.pptx
Exploratory Data Analysis - Dilip S.pptxExploratory Data Analysis - Dilip S.pptx
Exploratory Data Analysis - Dilip S.pptx
DilipVasan
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
StarCompliance.io
 
Machine Learning For Career Growth..pptx
Machine Learning For Career Growth..pptxMachine Learning For Career Growth..pptx
Machine Learning For Career Growth..pptx
benishzehra469
 

Recently uploaded (20)

AI Imagen for data-storytelling Infographics.pdf
AI Imagen for data-storytelling Infographics.pdfAI Imagen for data-storytelling Infographics.pdf
AI Imagen for data-storytelling Infographics.pdf
 
Pre-ProductionImproveddsfjgndflghtgg.pptx
Pre-ProductionImproveddsfjgndflghtgg.pptxPre-ProductionImproveddsfjgndflghtgg.pptx
Pre-ProductionImproveddsfjgndflghtgg.pptx
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
how can i exchange pi coins for others currency like Bitcoin
how can i exchange pi coins for others currency like Bitcoinhow can i exchange pi coins for others currency like Bitcoin
how can i exchange pi coins for others currency like Bitcoin
 
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
 
Exploratory Data Analysis - Dilip S.pptx
Exploratory Data Analysis - Dilip S.pptxExploratory Data Analysis - Dilip S.pptx
Exploratory Data Analysis - Dilip S.pptx
 
Tabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflowsTabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflows
 
Supply chain analytics to combat the effects of Ukraine-Russia-conflict
Supply chain analytics to combat the effects of Ukraine-Russia-conflictSupply chain analytics to combat the effects of Ukraine-Russia-conflict
Supply chain analytics to combat the effects of Ukraine-Russia-conflict
 
2024 Q2 Orange County (CA) Tableau User Group Meeting
2024 Q2 Orange County (CA) Tableau User Group Meeting2024 Q2 Orange County (CA) Tableau User Group Meeting
2024 Q2 Orange County (CA) Tableau User Group Meeting
 
Artificial_General_Intelligence__storm_gen_article.pdf
Artificial_General_Intelligence__storm_gen_article.pdfArtificial_General_Intelligence__storm_gen_article.pdf
Artificial_General_Intelligence__storm_gen_article.pdf
 
basics of data science with application areas.pdf
basics of data science with application areas.pdfbasics of data science with application areas.pdf
basics of data science with application areas.pdf
 
Using PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDBUsing PDB Relocation to Move a Single PDB to Another Existing CDB
Using PDB Relocation to Move a Single PDB to Another Existing CDB
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
 
Investigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_CrimesInvestigate & Recover / StarCompliance.io / Crypto_Crimes
Investigate & Recover / StarCompliance.io / Crypto_Crimes
 
How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?
 
2024 Q1 Tableau User Group Leader Quarterly Call
2024 Q1 Tableau User Group Leader Quarterly Call2024 Q1 Tableau User Group Leader Quarterly Call
2024 Q1 Tableau User Group Leader Quarterly Call
 
Machine Learning For Career Growth..pptx
Machine Learning For Career Growth..pptxMachine Learning For Career Growth..pptx
Machine Learning For Career Growth..pptx
 
Webinar One View, Multiple Systems No-Code Integration of Salesforce and ERPs
Webinar One View, Multiple Systems No-Code Integration of Salesforce and ERPsWebinar One View, Multiple Systems No-Code Integration of Salesforce and ERPs
Webinar One View, Multiple Systems No-Code Integration of Salesforce and ERPs
 
Slip-and-fall Injuries: Top Workers' Comp Claims
Slip-and-fall Injuries: Top Workers' Comp ClaimsSlip-and-fall Injuries: Top Workers' Comp Claims
Slip-and-fall Injuries: Top Workers' Comp Claims
 

integrity and cuncurrency in DBMS

  • 1. Advanced Database Systems andAdvanced Database Systems and Data WarehousingData Warehousing INTEGRITY AND CONCURRENCYINTEGRITY AND CONCURRENCY IN DATABASE SYSTEMSIN DATABASE SYSTEMS By: Benmammass MehdiBy: Benmammass Mehdi
  • 2. OutlineOutline ► IntegrityIntegrity  IntroductionIntroduction  Achieving integrity in a database systemAchieving integrity in a database system ► Integrity Subsystem ComponentIntegrity Subsystem Component ► Integrity RulesIntegrity Rules ► ConcurrencyConcurrency  IntroductionIntroduction  Some important definitionsSome important definitions  Lock-Based ProtocolsLock-Based Protocols  Deadlock Avoidance in lock-based protocolsDeadlock Avoidance in lock-based protocols  Locking GranularityLocking Granularity  Optimistic Concurrency ControlOptimistic Concurrency Control ► ConclusionConclusion
  • 3. Introduction - IntegrityIntroduction - Integrity ► The main features that a database system shouldThe main features that a database system should exhibit are :exhibit are :  AccuracyAccuracy  CorrectnessCorrectness  ValidityValidity ► An integrity constraint guards against accidentalAn integrity constraint guards against accidental damage of database.damage of database. ► It ensures data consistency by allowing onlyIt ensures data consistency by allowing only authorized changes in the database.authorized changes in the database. ► TheThe Integrity SubsystemIntegrity Subsystem is a component of theis a component of the DBMS.DBMS.
  • 4. OutlineOutline ► IntegrityIntegrity  IntroductionIntroduction  Achieving integrity in a database systemAchieving integrity in a database system ► Integrity Subsystem ComponentIntegrity Subsystem Component ► Integrity ConstraintsIntegrity Constraints ► ConcurrencyConcurrency  IntroductionIntroduction  Some important definitionsSome important definitions  Lock-Based ProtocolsLock-Based Protocols  Deadlock Avoidance in lock-based protocolsDeadlock Avoidance in lock-based protocols  Locking GranularityLocking Granularity  Optimistic Concurrency ControlOptimistic Concurrency Control ► ConclusionConclusion
  • 5. Integrity subsystemIntegrity subsystem ►The role of an integrity subsystem is :The role of an integrity subsystem is :  Monitoring transactions and detecting integrityMonitoring transactions and detecting integrity violations.violations.  Take appropriate actions given a violation.Take appropriate actions given a violation. ►The integrity subsystem is provided with aThe integrity subsystem is provided with a set of rules that define the following :set of rules that define the following :  the errors to check for;the errors to check for;  when to check for these errors;when to check for these errors;  what to do if an error occurs.what to do if an error occurs.
  • 6. Integrity RulesIntegrity Rules ►Set of rules stored in the system dictionarySet of rules stored in the system dictionary by Integrity Rule Compiler.by Integrity Rule Compiler. ►A new integrity rule, before being adopted,A new integrity rule, before being adopted, must fulfill all the existing rules.must fulfill all the existing rules. RULE#1 : AFTER UPDATING sales.quantity :RULE#1 : AFTER UPDATING sales.quantity : sales.quantity > 0sales.quantity > 0 ELSEELSE DO ;DO ; set return code to “RULE#1 violated” ;set return code to “RULE#1 violated” ; REJECT ;REJECT ; END ;END ;
  • 7. Integrity RulesIntegrity Rules ►The general structure of an integrity rule isThe general structure of an integrity rule is  Trigger condition (after updating, inserting…)Trigger condition (after updating, inserting…)  Constraint (sales.quantity >0)Constraint (sales.quantity >0)  Violation response (else do…)Violation response (else do…) ►There are three types of integrity rulesThere are three types of integrity rules  Domain Integrity RuleDomain Integrity Rule  The relation integrity rulesThe relation integrity rules  The fansets integrity constraintsThe fansets integrity constraints
  • 8. Domain Integrity Rule (1)Domain Integrity Rule (1) DCL S#DCL S# PRIMARY DOMAIN CHARACTER (5)PRIMARY DOMAIN CHARACTER (5) SUBSTR (S#,1,1) = ‘S’SUBSTR (S#,1,1) = ‘S’ AND IS_NUMERIC (SUBSTR (S#,2,4))AND IS_NUMERIC (SUBSTR (S#,2,4)) ELSEELSE DO;DO; Set return code to “S# domain ruleSet return code to “S# domain rule violated” ;violated” ; REJECT ;REJECT ; END ;END ; ► S# is a string of 5 characters. Te first character isS# is a string of 5 characters. Te first character is an S and the last 4 characters are numeric.an S and the last 4 characters are numeric.
  • 9. Domain Integrity Rule (2)Domain Integrity Rule (2) ►Composite domains : a domain DATE whichComposite domains : a domain DATE which is composed of three domains DAY,is composed of three domains DAY, MONTH and YEARMONTH and YEAR ►User-Written Procedures.User-Written Procedures. ►Interdomain Procedures : some conversionInterdomain Procedures : some conversion rules (procedures) may help for example torules (procedures) may help for example to compare two values from two distinctcompare two values from two distinct domains (distance expressed in kms anddomains (distance expressed in kms and miles).miles).
  • 10. Relation Integrity Rule (1)Relation Integrity Rule (1) ► Immediate record state constraintsImmediate record state constraints After updating or inserting sales.quantity, verify :After updating or inserting sales.quantity, verify : sales.quantity > 0sales.quantity > 0 ► Immediate record transition constraintsImmediate record transition constraints New_date > sales.dateNew_date > sales.date ► Immediate set state/transition constraintsImmediate set state/transition constraints  define a key uniqueness and enforcing non-null values ofdefine a key uniqueness and enforcing non-null values of the key (Entity Integrity Rule)the key (Entity Integrity Rule)  impose referential integrity (Foreign Key Integrity Rule)impose referential integrity (Foreign Key Integrity Rule)
  • 11. Relation Integrity Rule (2)Relation Integrity Rule (2) ►Deferred record state constraintsDeferred record state constraints ►Deferred record transition constraintsDeferred record transition constraints ►Deferred set state constraintsDeferred set state constraints Applied at the end of the transactionApplied at the end of the transaction ((WHEN COMMITINGWHEN COMMITING). We need this kind). We need this kind of constraints because sometimes the setof constraints because sometimes the set of updates in a transaction violatesof updates in a transaction violates temporarily the rule.temporarily the rule. ►Deferred set transition constraintsDeferred set transition constraints
  • 12. Other Integrity ConstraintsOther Integrity Constraints ► Fanset Integrity RulesFanset Integrity Rules  Used in network databases. They prevent integrityUsed in network databases. They prevent integrity violations by providing referential integrity.violations by providing referential integrity. ► Triggered proceduresTriggered procedures  Integrity rules are special case of triggered procedures.Integrity rules are special case of triggered procedures.  Are useful to carry out the following tasks :Are useful to carry out the following tasks : ► Prevent the user that deleting a client will delete all its sales.Prevent the user that deleting a client will delete all its sales. ► Access security.Access security. ► Performance measurement of the database.Performance measurement of the database. ► Controlling stored record (compressing and decompressingControlling stored record (compressing and decompressing data when storing and retrieving data).data when storing and retrieving data). ► Exception reporting (expiry date for medicaments)Exception reporting (expiry date for medicaments)
  • 13. OutlineOutline ► IntegrityIntegrity  IntroductionIntroduction  Achieving integrity in a database systemAchieving integrity in a database system ► Integrity Subsystem ComponentIntegrity Subsystem Component ► Integrity ConstraintsIntegrity Constraints ► ConcurrencyConcurrency  IntroductionIntroduction  Some important definitionsSome important definitions  Lock-Based ProtocolsLock-Based Protocols  Deadlock Avoidance in lock-based protocolsDeadlock Avoidance in lock-based protocols  Locking GranularityLocking Granularity  Optimistic Concurrency ControlOptimistic Concurrency Control ► ConclusionConclusion
  • 14. Concurrency Control - IntroductionConcurrency Control - Introduction ► ContentionContention occurs when two or more users try tooccurs when two or more users try to access simultaneously the same record.access simultaneously the same record. ► ConcurrencyConcurrency occurs when multiple users haveoccurs when multiple users have the ability to access the same resource and eachthe ability to access the same resource and each user has access to the resource in isolation.user has access to the resource in isolation. Concurrency is high when there is no apparentConcurrency is high when there is no apparent wait time for a user to get its request. Concurrencywait time for a user to get its request. Concurrency is low when wait times are evidentis low when wait times are evident ► ConsistencyConsistency occurs when users access aoccurs when users access a shared resource and the resource exhibits theshared resource and the resource exhibits the same characteristics and satisfies all thesame characteristics and satisfies all the constraints among all operations.constraints among all operations.
  • 15. Concurrency Control - IntroductionConcurrency Control - Introduction ► Example (Bank transactions) :Example (Bank transactions) :  2 accounts A and B (assume balances A and2 accounts A and B (assume balances A and B=100DH)B=100DH)  2 transactions T1 and T2 that will be executed2 transactions T1 and T2 that will be executed concurrentlyconcurrently ► T1T1 : start, A=A+100, B=B-100, COMMIT: start, A=A+100, B=B-100, COMMIT ► T2T2 : start, A=A*1.05, B=B*1.05, COMMIT: start, A=A*1.05, B=B*1.05, COMMIT
  • 16. Concurrency Control - IntroductionConcurrency Control - Introduction ►T1T1 A=A+100 B=B-100B=B-100 COMMITCOMMIT ►T2T2 A=A*1.05A=A*1.05 B=B*1.05B=B*1.05 COMMITCOMMIT ►T1T1 A+100A+100 ►T2T2 A=A*1.05A=A*1.05 B=B*1.05B=B*1.05 COMMITCOMMIT ►T1T1 B=B-100B=B-100 COMMITCOMMIT A = 210 DHA = 210 DH B = 0 DHB = 0 DH A = 210 DHA = 210 DH B = 5 DHB = 5 DH Consider these two different sequences ofConsider these two different sequences of execution :execution : INTERFERENCINTERFERENC EE
  • 17. Concurrency Control - SolutionsConcurrency Control - Solutions ►Lock-Based ProtocolsLock-Based Protocols ►Timestamp TechniquesTimestamp Techniques ►Optimistic Concurrency ControlOptimistic Concurrency Control
  • 18. Concurrency Control – LockConcurrency Control – Lock ManagerManager► AA Lock managerLock manager can be implemented as a separatecan be implemented as a separate process to which transactions send lock and unlockprocess to which transactions send lock and unlock requestsrequests ► The lock manager replies to a lock request by sendingThe lock manager replies to a lock request by sending a lock grant messages (or a message asking thea lock grant messages (or a message asking the transaction to roll back, in case of a deadlock)transaction to roll back, in case of a deadlock) ► The requesting transaction waits until its request isThe requesting transaction waits until its request is answeredanswered ► The lock manager maintains a data structure called aThe lock manager maintains a data structure called a lock tablelock table to record granted locks and pendingto record granted locks and pending requestsrequests
  • 19. Concurrency – LB Protocols (1)Concurrency – LB Protocols (1) Principle :Principle : ► Transactions ask for a lock on a record beforeTransactions ask for a lock on a record before updating it.updating it. ► After update, the record is unlockedAfter update, the record is unlocked ► We have two types of locks :We have two types of locks :  exclusive (X) modeexclusive (X) mode: data can be both read as well as: data can be both read as well as written. X-lock is requested usingwritten. X-lock is requested using lock-Xlock-X instruction.instruction. Records are the unit of locking.Records are the unit of locking.  shared (S) modeshared (S) mode: data can only be read. S-lock is: data can only be read. S-lock is requested usingrequested using lock-Slock-S instruction. Tables are the unitinstruction. Tables are the unit of locking.of locking.
  • 20. LB Protocols (2)LB Protocols (2) Transaction A Shared Lock Exclusive Lock Exclusive Lock Accounts Table Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Transaction B Shared Lock Exclusive Lock Exclusive Lock Shared and Exclusive LocksShared and Exclusive Locks
  • 21. LB Protocols (3)LB Protocols (3) ► Compatibility MatrixCompatibility Matrix Shared lock Exclusive lock Shared lock True False Exclusive lock False False
  • 22. Lock TableLock Table ► Black rectangles indicate grantedBlack rectangles indicate granted locks, white ones indicate waitinglocks, white ones indicate waiting requestsrequests ► Lock table also records the type ofLock table also records the type of lock granted or requestedlock granted or requested ► New request is added to the endNew request is added to the end of the queue of requests for theof the queue of requests for the data item, and granted if it isdata item, and granted if it is compatible with all earlier lockscompatible with all earlier locks ► Unlock requests result in theUnlock requests result in the request being deleted, and laterrequest being deleted, and later requests are checked to see ifrequests are checked to see if they can now be grantedthey can now be granted ► If transaction aborts, all waiting orIf transaction aborts, all waiting or granted requests of thegranted requests of the transaction are deletedtransaction are deleted  lock manager may keep a listlock manager may keep a list of locks held by eachof locks held by each transaction, to implement thistransaction, to implement this efficientlyefficiently
  • 23. LB Protocols (4) – PX ProtocolLB Protocols (4) – PX Protocol ►Any transaction that intends to update aAny transaction that intends to update a record must first execute an exclusive lockrecord must first execute an exclusive lock request (X-lock) on that record.request (X-lock) on that record. ►If the lock cannot be acquired, theIf the lock cannot be acquired, the transaction goes into a wait state.transaction goes into a wait state. ►When the record becomes available, theWhen the record becomes available, the lock can be granted and the transaction canlock can be granted and the transaction can resume processing.resume processing.
  • 24. LB Protocols (5) – PX ProtocolLB Protocols (5) – PX Protocol ► Example : 2 transactionsExample : 2 transactions Transaction 1:Transaction 1: Transaction 2 :Transaction 2 : lock-X(B)lock-X(B) lock-lock- S(A)S(A) read(B)read(B) read(A)read(A) B = B -50B = B -50 unlock(A)unlock(A) write(B)write(B) lock-S(B)lock-S(B) unlock(B)unlock(B) read(B)read(B) lock-X(A)lock-X(A) unlock(B)unlock(B) read(A)read(A) display(A+B)display(A+B) A = A + 50A = A + 50 write(A)write(A) unlock(A)unlock(A)
  • 25. LB Protocols (6) – PX ProtocolLB Protocols (6) – PX Protocol Execution sequence :Execution sequence : Transaction 1Transaction 1 Transaction 2Transaction 2 Concurrency control managerConcurrency control manager lock-X(B)lock-X(B) grant-X(B)grant-X(B) read(B)read(B) B = B -50B = B -50 write(B)write(B) unlock(B)unlock(B) lock-S(A)lock-S(A) grant-S(A)grant-S(A) read(A)read(A) unlock(A)unlock(A) lock-S(B)lock-S(B) grant-S(B)grant-S(B) read(B)read(B) unlock(B)unlock(B) display(A+B)display(A+B) lock-X(A)lock-X(A) grant-X(A)grant-X(A) read(A)read(A) A = A + 50A = A + 50 write(A)write(A) unlock(A)unlock(A)
  • 26. LB Protocols (7) – PX ProtocolLB Protocols (7) – PX Protocol ► Serializability :Serializability : interleaved execution sequence ofinterleaved execution sequence of a set of transactions that will obtain the samea set of transactions that will obtain the same results as if the transactions are processedresults as if the transactions are processed serially.serially. ► We have to look at the lock requests of eachWe have to look at the lock requests of each transaction and to find an order to execute themtransaction and to find an order to execute them without any interference between then. Thewithout any interference between then. The resulting sequence, if there is one, implies that theresulting sequence, if there is one, implies that the two transactions are serializable.two transactions are serializable. ► PX Protocol then can be applied.PX Protocol then can be applied.
  • 27. LB Protocols (7) – PX ProtocolLB Protocols (7) – PX Protocol ► Using the lock-based mechanism, deadlock andUsing the lock-based mechanism, deadlock and starvation can occur. This is an example ofstarvation can occur. This is an example of deadlock :deadlock : Transaction A Shared Lock Already X- Locked Asks for an X- Lock Accounts Table Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Transaction B Shared Lock Asks for an X- Lock Already X- Locked
  • 28. LB Protocols – PXC ProtocolLB Protocols – PXC Protocol ►Derived from PX protocol.Derived from PX protocol. ►Exclusive locks are retained until end ofExclusive locks are retained until end of transaction (COMMIT or ROLLBACK).transaction (COMMIT or ROLLBACK). ►PXC helps to avoid loss of updates bcausePXC helps to avoid loss of updates bcause of ROLLBACK.of ROLLBACK.  No transaction is allowed to update anNo transaction is allowed to update an uncommitted changed record.uncommitted changed record.
  • 29. LB Protocols – PS / PSCLB Protocols – PS / PSC ProtocolsProtocols ► Any transaction that updates a record must firstlyAny transaction that updates a record must firstly ask for a shared lock of that record.ask for a shared lock of that record. ► During the transaction, just before the updateDuring the transaction, just before the update command, comes a request of changing the lock-Scommand, comes a request of changing the lock-S to lock-X.to lock-X. ► A transaction should not be allowed to lock itselfA transaction should not be allowed to lock itself out.out. ► The goal here is to limit the duration of X-locks.The goal here is to limit the duration of X-locks.
  • 30. LB Protocols – PS ProtocolLB Protocols – PS Protocol ►Example (here deadlock occurs at T4):Example (here deadlock occurs at T4):  Transaction ATransaction A Transaction BTransaction B SFIND Record1SFIND Record1 ---- ---- ---- ---- SFIND R1SFIND R1 UPD Record1 -- ---- ---- ---- UPD R1UPD R1 TimeTime T1T1 T2T2 T3T3 T4T4
  • 31. LB Protocols – PU / PUCLB Protocols – PU / PUC ProtocolProtocol►This protocol uses a third lock state : theThis protocol uses a third lock state : the update lockupdate lock.. ►Any transaction that intends to update aAny transaction that intends to update a record is required to ask for U-lock of thatrecord is required to ask for U-lock of that record. A U-lock is compatible with an S-record. A U-lock is compatible with an S- lock but not with another U-lock.lock but not with another U-lock. ►Replacing S-locks by U-locks will preventReplacing S-locks by U-locks will prevent deadlock.deadlock.
  • 32. LB Protocols – PU / PUCLB Protocols – PU / PUC ProtocolProtocol ► Compatibility matrix :Compatibility matrix : X S U X False False False S False True True U False True False ►ExampleExample : compare PU and PS protocols: compare PU and PS protocols
  • 33. LB Protocols – PU ProtocolLB Protocols – PU Protocol ►This protocol is more efficient than theThis protocol is more efficient than the previous ones.previous ones. ►It limits considerably deadlock, because itIt limits considerably deadlock, because it decreases the number of S-locks.decreases the number of S-locks.
  • 34. LB Protocols –Two Phase LockingLB Protocols –Two Phase Locking ProtocolProtocol ► 2PL2PL ensures conflict-serializable schedules.ensures conflict-serializable schedules. ► 2PL includes two phases :2PL includes two phases :  Growing phaseGrowing phase : transaction may obtain locks and: transaction may obtain locks and may not release locksmay not release locks  Shrinking phaseShrinking phase : transaction may release locks and: transaction may release locks and may not obtain locks.may not obtain locks. ► The schedule is determined in the relation toThe schedule is determined in the relation to thethe order of theirorder of their lock pointslock points.. ► If all transactions are two-phase, then allIf all transactions are two-phase, then all executions are serializable.executions are serializable.
  • 35. LB Protocols –2PL ProtocolLB Protocols –2PL Protocol Example :Example :
  • 36. LB Protocols –2PL ProtocolLB Protocols –2PL Protocol ► There are many protocols derived from 2PL :There are many protocols derived from 2PL :  Strict two-phase lockingStrict two-phase locking . Here a transaction must. Here a transaction must hold all its exclusive locks till it commits.hold all its exclusive locks till it commits.  Rigorous two-phase lockingRigorous two-phase locking is even stricter: hereis even stricter: here allall locks (shared and exclusive) are held till commit. Inlocks (shared and exclusive) are held till commit. In this protocol transactions can be serialized in the orderthis protocol transactions can be serialized in the order in which they commit.in which they commit.  Graph-based protocol :Graph-based protocol : we fix an order of accessingwe fix an order of accessing data. If a transaction has to update Row2 and readdata. If a transaction has to update Row2 and read Row1, it has to access these data in a predefined order.Row1, it has to access these data in a predefined order.
  • 37. LB Protocols – Deadlock AvoidanceLB Protocols – Deadlock Avoidance ►Deadlock preventionDeadlock prevention protocols ensure thatprotocols ensure that the system will never enter into a deadlockthe system will never enter into a deadlock state. It can be achieved using differentstate. It can be achieved using different strategies :strategies :  Transaction SchedulingTransaction Scheduling  Request RejectionRequest Rejection  Transaction RetryTransaction Retry
  • 38. LB-Protocols : Deadlock PreventionLB-Protocols : Deadlock Prevention StrategiesStrategies ►Timeout-Based SchemesTimeout-Based Schemes ::  a transaction waits for a lock only for a specifieda transaction waits for a lock only for a specified amount of time. After that, the wait times outamount of time. After that, the wait times out and the transaction is rolled back.and the transaction is rolled back.  thus deadlocks are not possiblethus deadlocks are not possible  simple to implement; but starvation is possible.simple to implement; but starvation is possible. Also difficult to determine good value of theAlso difficult to determine good value of the timeout interval.timeout interval.
  • 39. LB-Protocols : Deadlock PreventionLB-Protocols : Deadlock Prevention StrategiesStrategies ► What to do when a deadlock is detected ?What to do when a deadlock is detected ? ► Some transactions will have to roll back to breakSome transactions will have to roll back to break deadlock. Select that transaction as victim thatdeadlock. Select that transaction as victim that will incur minimum cost.will incur minimum cost. ► We have to determine how far to roll back theWe have to determine how far to roll back the transaction. We can either carry out :transaction. We can either carry out :  Total rollback: Abort the transaction and then restart it.Total rollback: Abort the transaction and then restart it.  Partial rollback: it is more effective to roll backPartial rollback: it is more effective to roll back transaction only as far as necessary to break deadlocktransaction only as far as necessary to break deadlock
  • 40. Deadlock Avoidance : Transaction SchedulingDeadlock Avoidance : Transaction Scheduling ►Two transactions will not be runTwo transactions will not be run concurrently if their data requirementsconcurrently if their data requirements conflict.conflict. ►WeWe mustmust know what are the dataknow what are the data requirements of each transaction before runrequirements of each transaction before run time => impossible till runtime.time => impossible till runtime. ►Consequently, the lock unit is a set ofConsequently, the lock unit is a set of records and locks are applied at transactionrecords and locks are applied at transaction initiation instead of during execution.initiation instead of during execution.
  • 41. Deadlock Avoidance : Request RejectionDeadlock Avoidance : Request Rejection ►The system rejects any lock request thatThe system rejects any lock request that cannot be applied.cannot be applied. ►It uses theIt uses the deadlock detection algorithmdeadlock detection algorithm.. ►When trying to grant a lock request, if aWhen trying to grant a lock request, if a deadlock is detected, the transaction isdeadlock is detected, the transaction is rejected.rejected.
  • 42. Deadlock Avoidance : Transaction RetryDeadlock Avoidance : Transaction Retry ► Transactions are timestamped with their start time.Transactions are timestamped with their start time. ► Example : A requests a lock on a record already locked by BExample : A requests a lock on a record already locked by B ► wait-diewait-die scheme — non-preemptivescheme — non-preemptive  A waits if it is older than B, otherwise, itA waits if it is older than B, otherwise, it diesdies and it is rolled back andand it is rolled back and automatically retried.automatically retried.  a transaction may die several times before acquiring its needed dataa transaction may die several times before acquiring its needed data itemitem ► wound-waitwound-wait scheme — preemptivescheme — preemptive  A waits if it is younger than B, otherwise itA waits if it is younger than B, otherwise it woundswounds (forces rollback) of(forces rollback) of younger transaction instead of waiting for it.younger transaction instead of waiting for it.  Younger transactions mayYounger transactions may waitwait for older ones.for older ones.  Less rollbacks thanLess rollbacks than wait-diewait-die scheme.scheme. ► Transactions retain their timestamps even if they are rolledTransactions retain their timestamps even if they are rolled back.back.
  • 43. LB-Protocols : Deadlock Detection AlgorithmLB-Protocols : Deadlock Detection Algorithm ►The system is in a deadlock state if and onlyThe system is in a deadlock state if and only ifif the wait-for graph has a cycle.the wait-for graph has a cycle. ►The system must invoke a deadlock-The system must invoke a deadlock- detection algorithmdetection algorithm periodicallyperiodically to look forto look for cycles.cycles.
  • 44. LB-Protocols : Deadlock Detection AlgorithmLB-Protocols : Deadlock Detection Algorithm Wait-for graph without a cycle Wait-for graph with a cycle BackBack
  • 45. LB Protocols : LockingLB Protocols : Locking GranularityGranularity ► Allow data items to be of various sizes and defineAllow data items to be of various sizes and define a hierarchy of data granularities, where the smalla hierarchy of data granularities, where the small granularities are nested within larger ones.granularities are nested within larger ones. ► Can be represented graphically as a tree. When aCan be represented graphically as a tree. When a transaction locks a node in the treetransaction locks a node in the tree explicitlyexplicitly, it, it implicitlyimplicitly locks all the node's descendents in thelocks all the node's descendents in the same mode.same mode. ► Granularity of lockingGranularity of locking (level in tree where locking(level in tree where locking is done):is done):  fine granularityfine granularity (lower in tree): high concurrency, high(lower in tree): high concurrency, high locking overheadlocking overhead  coarse granularitycoarse granularity (higher in tree): low locking(higher in tree): low locking overhead, low concurrencyoverhead, low concurrency
  • 46. LB Protocols : LockingLB Protocols : Locking GranularityGranularity ►The highest level in the example hierarchy is theThe highest level in the example hierarchy is the entire database.entire database. ►The levels below are of typeThe levels below are of type areaarea,, filefile andand recordrecord in that order.in that order.
  • 47. LB Protocols : Intent Locking ProtocolLB Protocols : Intent Locking Protocol ► In addition to S and X lock modes, there are threeIn addition to S and X lock modes, there are three additional lock modes with multiple granularity:additional lock modes with multiple granularity:  intention-sharedintention-shared (IS): indicates explicit locking at a(IS): indicates explicit locking at a lower level of the tree but only with shared locks.lower level of the tree but only with shared locks.  intentionintention--exclusiveexclusive (IX): indicates explicit locking at a(IX): indicates explicit locking at a lower level with exclusive or shared lockslower level with exclusive or shared locks  shared and intentionshared and intention--exclusiveexclusive (SIX): the subtree(SIX): the subtree rooted by that node is locked explicitly in shared moderooted by that node is locked explicitly in shared mode and explicit locking is being done at a lower level withand explicit locking is being done at a lower level with exclusive-mode locks.exclusive-mode locks. ► Intention locks allow a higher level node to beIntention locks allow a higher level node to be locked in S or X mode without having to check alllocked in S or X mode without having to check all descendent nodes.descendent nodes.
  • 48. LB Protocols : Intent Locking ProtocolLB Protocols : Intent Locking Protocol IS IX S S IX X IS IX S S IX X     ×     × × × × × × ×× × × × × × ×× Compatibility Matrix :Compatibility Matrix :
  • 49. LB Protocols : Intent Locking ProtocolLB Protocols : Intent Locking Protocol ► TransactionTransaction TTii can lock a nodecan lock a node QQ, using the following rules:, using the following rules: 1. The lock compatibility matrix must be observed.1. The lock compatibility matrix must be observed. 2. The root of the tree must be locked first, and may be locked in any2. The root of the tree must be locked first, and may be locked in any mode.mode. 3. A node3. A node QQ can be locked bycan be locked by TTii in S or IS mode only if the parentin S or IS mode only if the parent ofof QQ is currently locked byis currently locked by TTii in either IX or IS mode.in either IX or IS mode. 4. A node4. A node QQ can be locked bycan be locked by TTii in X, SIX, or IX mode only if thein X, SIX, or IX mode only if the parent ofparent of QQ is currently locked byis currently locked by TTii in either IX or SIX mode.in either IX or SIX mode. 5.5. TTii can lock a node only if it has not previously unlocked any nodecan lock a node only if it has not previously unlocked any node (that is,(that is, TTii is two-phase).is two-phase). 6.6. TTii can unlock a nodecan unlock a node QQ only if none of the children ofonly if none of the children of QQ are currentlyare currently locked bylocked by TTii.. ► Locks are acquired in root-to-leaf order, whereas they are released inLocks are acquired in root-to-leaf order, whereas they are released in leaf-to-root order.leaf-to-root order.
  • 50. LB ProtocolsLB Protocols ►Default Locking Behavior for OracleDefault Locking Behavior for Oracle  A pure SELECT will not lock any row.A pure SELECT will not lock any row.  INSERT, UPDATE or DELETE will place a rowINSERT, UPDATE or DELETE will place a row Exclusive LockExclusive Lock (X-lock).(X-lock).  SELECT...FROM...FOR UPDATE will place aSELECT...FROM...FOR UPDATE will place a row Shared Lock (S-lock).row Shared Lock (S-lock).
  • 51. LB ProtocolsLB Protocols ► Oracle Syntax:Oracle Syntax: LOCK TABLE [LOCK TABLE [schema.schema.] table [] table [optionsoptions] IN] IN lock modelock mode MODE [NOWAIT]MODE [NOWAIT]  Options:Options: ► PARTITION partitionPARTITION partition ► SUBPARTITION subpartitionSUBPARTITION subpartition ► @dblink@dblink  Lock modes:Lock modes: ► EXCLUSIVEEXCLUSIVE ► SHARESHARE ► ROW EXCLUSIVEROW EXCLUSIVE ► SHARE ROW EXCLUSIVESHARE ROW EXCLUSIVE ► ROW SHARE* | SHARE UPDATE*ROW SHARE* | SHARE UPDATE*
  • 52. Optimistic Concurrency ControlOptimistic Concurrency Control ReadRead ValidationValidation WriteWrite ► A transaction in OCC is composed of three phases :A transaction in OCC is composed of three phases :  Read PhaseRead Phase Transactions access the database to load data,Transactions access the database to load data, then they update data in a separate buffer.then they update data in a separate buffer.  Validation phaseValidation phase For each transaction, the system checks if there isFor each transaction, the system checks if there is any conflict with another transaction. If there is, the transactionany conflict with another transaction. If there is, the transaction is rolled back, otherwise the write phase can proceed.is rolled back, otherwise the write phase can proceed.  Write phaseWrite phase Updates are written from the buffer to the database.Updates are written from the buffer to the database.
  • 53. CONCLUSIONCONCLUSION ►Locking is a pessimist concurrency control,Locking is a pessimist concurrency control, because it assumes maximum contention.because it assumes maximum contention. ►OCC is dead-lock free because it does notOCC is dead-lock free because it does not implement locking.implement locking.
  • 54. ReferencesReferences ► Date BookDate Book ► Cooperative Cataloging in a Scalable Digital LibraryCooperative Cataloging in a Scalable Digital Library SystemSystem, Dr Hachim Haddouti, Dr Hachim Haddouti ► Transaction ManagementTransaction Management, IBM Research Laboratory, San, IBM Research Laboratory, San Jose CaliforniaJose California ► Performance ofPerformance of ConcurrencyConcurrency ControlControl Mechanisms inMechanisms in Centralized Database SystemsCentralized Database Systems, Vijay Kumar., Vijay Kumar. ► Overview ofOverview of concurrencyconcurrency controlcontrol and locking forand locking for databasesdatabases.. www.odbmsfacts.comwww.odbmsfacts.com/articles//articles/ concurrency_control_and_locking.htmlconcurrency_control_and_locking.html
  • 55. Q&AQ&A Integrity and ConcurrencyIntegrity and Concurrency Control in Database SystemControl in Database System