Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By:...

55
Advanced Database Systems Advanced Database Systems and Data Warehousing and Data Warehousing INTEGRITY AND CONCURRENCY INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS IN DATABASE SYSTEMS By: Benmammass Mehdi By: Benmammass Mehdi
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    225
  • download

    3

Transcript of Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By:...

Page 1: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Advanced Database Systems Advanced Database Systems and Data Warehousingand Data Warehousing

INTEGRITY AND CONCURRENCY INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS IN DATABASE SYSTEMS

By: Benmammass MehdiBy: Benmammass Mehdi

Page 2: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

OutlineOutline► IntegrityIntegrity

Introduction Introduction Achieving integrity in a database system Achieving 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 Granularity Locking Granularity Optimistic Concurrency ControlOptimistic Concurrency Control

► ConclusionConclusion

Page 3: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Introduction - IntegrityIntroduction - Integrity► The main features that a database system The main features that a database system

should exhibit are :should exhibit are : AccuracyAccuracy CorrectnessCorrectness ValidityValidity

► An integrity constraint guards against An integrity constraint guards against accidental damage of database. accidental damage of database.

► It ensures data consistency by allowing only It ensures data consistency by allowing only authorized changes in the database.authorized changes in the database.

► The The Integrity SubsystemIntegrity Subsystem is a component of is a component of the DBMS.the DBMS.

Page 4: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

OutlineOutline► IntegrityIntegrity

Introduction Introduction Achieving integrity in a database system Achieving 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 Granularity Locking Granularity Optimistic Concurrency ControlOptimistic Concurrency Control

► ConclusionConclusion

Page 5: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Integrity subsystemIntegrity subsystem►The role of an integrity subsystem is :The role of an integrity subsystem is :

Monitoring transactions and detecting Monitoring transactions and detecting integrity violations.integrity violations.

Take appropriate actions given a violation.Take appropriate actions given a violation.

►The integrity subsystem is provided with The integrity subsystem is provided with a set of rules that define the following :a 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.

Page 6: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Integrity RulesIntegrity Rules►Set of rules stored in the system Set of rules stored in the system

dictionary by Integrity Rule Compiler.dictionary by Integrity Rule Compiler.►A new integrity rule, before being A new integrity rule, before being

adopted, must fulfill all the existing adopted, must fulfill all the existing rules.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 ;

Page 7: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Integrity RulesIntegrity Rules►The general structure of an integrity rule The general structure of an integrity rule

isis 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

Page 8: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

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 Set return code to “S# domain rule rule violated” ;violated” ;

REJECT ;REJECT ; END ;END ;

► S# is a string of 5 characters. Te first S# is a string of 5 characters. Te first character is an S and the last 4 characters are character is an S and the last 4 characters are numeric.numeric.

Page 9: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Domain Integrity Rule (2)Domain Integrity Rule (2)

►Composite domains : a domain DATE Composite domains : a domain DATE which is composed of three domains which is composed of three domains DAY, MONTH and YEARDAY, MONTH and YEAR

►User-Written Procedures.User-Written Procedures.► Interdomain Procedures : some Interdomain Procedures : some

conversion rules (procedures) may conversion rules (procedures) may help for example to compare two help for example to compare two values from two distinct domains values from two distinct domains (distance expressed in kms and miles).(distance expressed in kms and miles).

Page 10: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Relation Integrity Rule (1)Relation Integrity Rule (1)

► Immediate record state constraintsImmediate record state constraintsAfter 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 define a key uniqueness and enforcing non-null values of the key (Entity Integrity Rule)values of the key (Entity Integrity Rule)

impose referential integrity (Foreign Key Integrity impose referential integrity (Foreign Key Integrity Rule)Rule)

Page 11: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

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 transaction Applied at the end of the transaction ((WHEN COMMITINGWHEN COMMITING). We need this ). We need this

kind kind of constraints because of constraints because sometimes the set sometimes the set of updates in a of updates in a transaction violates transaction violates temporarily the temporarily the rule. rule.

►Deferred set transition constraintsDeferred set transition constraints

Page 12: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Other Integrity ConstraintsOther Integrity Constraints► Fanset Integrity Rules Fanset Integrity Rules

Used in network databases. They prevent integrity Used 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 Integrity rules are special case of triggered

procedures.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 Prevent the user that deleting a client will delete all its sales.sales.

► Access security.Access security.► Performance measurement of the database.Performance measurement of the database.► Controlling stored record (compressing and Controlling stored record (compressing and

decompressing data when storing and retrieving data).decompressing data when storing and retrieving data).► Exception reporting (expiry date for medicaments)Exception reporting (expiry date for medicaments)

Page 13: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

OutlineOutline► IntegrityIntegrity

Introduction Introduction Achieving integrity in a database system Achieving 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 Granularity Locking Granularity Optimistic Concurrency ControlOptimistic Concurrency Control

► ConclusionConclusion

Page 14: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Concurrency Control - Concurrency Control - IntroductionIntroduction► ContentionContention occurs when two or more users occurs when two or more users

try to access simultaneously the same record.try to access simultaneously the same record.► ConcurrencyConcurrency occurs when multiple users occurs when multiple users

have the ability to access the same resource have the ability to access the same resource and each user has access to the resource in and each user has access to the resource in isolation. Concurrency is high when there is no isolation. Concurrency is high when there is no apparent wait time for a user to get its apparent wait time for a user to get its request. Concurrency is low when wait times request. Concurrency is low when wait times are evidentare evident

► ConsistencyConsistency occurs when users access a occurs when users access a shared resource and the resource exhibits the shared resource and the resource exhibits the same characteristics and satisfies all the same characteristics and satisfies all the constraints among all operations.constraints among all operations.

Page 15: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Concurrency Control - Concurrency Control - IntroductionIntroduction► Example (Bank transactions) :Example (Bank transactions) :

2 accounts A and B (assume balances A 2 accounts A and B (assume balances A and B=100DH)and B=100DH)

2 transactions T1 and T2 that will be 2 transactions T1 and T2 that will be executed concurrentlyexecuted concurrently

► 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

Page 16: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Concurrency Control - Concurrency Control - IntroductionIntroduction

►T1 T1 A=A+100

B=B-100B=B-100COMMITCOMMIT

►T2T2A=A*1.05A=A*1.05B=B*1.05B=B*1.05COMMITCOMMIT

►T1T1A+100A+100

►T2T2A=A*1.05A=A*1.05B=B*1.05B=B*1.05COMMITCOMMIT

►T1T1B=B-100B=B-100COMMITCOMMIT

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 of Consider these two different sequences of execution :execution :

INTERFERENCINTERFERENCEE

Page 17: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Concurrency Control - Concurrency Control - SolutionsSolutions►Lock-Based ProtocolsLock-Based Protocols

►Timestamp Techniques Timestamp Techniques

►Optimistic Concurrency ControlOptimistic Concurrency Control

Page 18: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Concurrency Control – Lock Concurrency Control – Lock ManagerManager► AA Lock manager Lock manager can be implemented as a can be implemented as a

separate process to which transactions send separate process to which transactions send lock and unlock requestslock and unlock requests

► The lock manager replies to a lock request by The lock manager replies to a lock request by sending a lock grant messages (or a message sending a lock grant messages (or a message asking the transaction to roll back, in case of a asking the transaction to roll back, in case of a deadlock)deadlock)

► The requesting transaction waits until its The requesting transaction waits until its request is answeredrequest is answered

► The lock manager maintains a data structure The lock manager maintains a data structure called a called a lock table lock table to record granted locks and to record granted locks and pending requestspending requests

Page 19: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Concurrency – LB Protocols Concurrency – LB Protocols (1)(1)

Principle :Principle :► Transactions ask for a lock on a record Transactions ask for a lock on a record

before updating it.before updating it.► After update, the record is unlocked After 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 : data can be both read as well as written. X-lock is requested using well as written. X-lock is requested using lock-X lock-X instruction. Records are the unit of locking. instruction. 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 using requested using lock-S lock-S instruction. Tables are instruction. Tables are the unit of locking.the unit of locking.

Page 20: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols (2)LB Protocols (2)

Transaction A

Shared Lock

Exclusive Lock

Exclusive Lock

Accounts Table

Row 1Row 2Row 3Row 4Row 5Row 6Row 7Row 8

Transaction B

Shared Lock

Exclusive Lock

Exclusive Lock

Shared and Exclusive LocksShared and Exclusive Locks

Page 21: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols (3)LB Protocols (3)

► Compatibility MatrixCompatibility Matrix

Shared lock Exclusive lock

Shared lock True False

Exclusive lock False False

Page 22: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Lock TableLock Table► Black rectangles indicate Black rectangles indicate

granted locks, white ones granted locks, white ones indicate waiting requestsindicate waiting requests

► Lock table also records the type Lock table also records the type of lock granted or requestedof lock granted or requested

► New request is added to the New request is added to the end of the queue of requests end of the queue of requests for the data item, and granted for the data item, and granted if it is compatible with all earlier if it is compatible with all earlier lockslocks

► Unlock requests result in the Unlock requests result in the request being deleted, and request being deleted, and later requests are checked to later requests are checked to see if they can now be grantedsee if they can now be granted

► If transaction aborts, all waiting If transaction aborts, all waiting or granted requests of the or granted requests of the transaction are deleted transaction are deleted lock manager may keep a lock manager may keep a

list of locks held by each list of locks held by each transaction, to implement transaction, to implement this efficientlythis efficiently

Page 23: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols (4) – PX ProtocolLB Protocols (4) – PX Protocol

►Any transaction that intends to update Any transaction that intends to update a record must first execute an exclusive a record must first execute an exclusive lock request (X-lock) on that record.lock request (X-lock) on that record.

► If the lock cannot be acquired, the If the lock cannot be acquired, the transaction goes into a wait state.transaction goes into a wait state.

►When the record becomes available, When the record becomes available, the lock can be granted and the the lock can be granted and the transaction can resume processing.transaction can resume processing.

Page 24: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

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-S(A)lock-S(A) read(B) read(B) read(A)read(A)

B = B -50 B = 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)

Page 25: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols (6) – PX ProtocolLB Protocols (6) – PX Protocol

Execution sequence :Execution sequence :

Transaction 1Transaction 1 Transaction 2 Transaction 2 Concurrency Concurrency control managercontrol manager

lock-X(B)lock-X(B) grant-X(B)grant-X(B)read(B)read(B)B = B -50B = B -50write(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)

Page 26: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols (7) – PX ProtocolLB Protocols (7) – PX Protocol

► Serializability : Serializability : interleaved execution interleaved execution sequence of a set of transactions that will sequence of a set of transactions that will obtain the same results as if the transactions obtain the same results as if the transactions are processed serially.are processed serially.

► We have to look at the lock requests of each We have to look at the lock requests of each transaction and to find an order to execute transaction and to find an order to execute them without any interference between then. them without any interference between then. The resulting sequence, if there is one, implies The resulting sequence, if there is one, implies that the two transactions are serializable.that the two transactions are serializable.

► PX Protocol then can be applied.PX Protocol then can be applied.

Page 27: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols (7) – PX ProtocolLB Protocols (7) – PX Protocol

► Using the lock-based mechanism, deadlock Using the lock-based mechanism, deadlock and starvation can occur. This is an example and starvation can occur. This is an example of deadlock : of deadlock :

Transaction A

Shared Lock

Already X-Locked

Asks for an X-Lock

Accounts Table

Row 1Row 2Row 3Row 4Row 5Row 6Row 7Row 8

Transaction B

Shared Lock

Asks for an X-Lock

Already X-Locked

Page 28: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols – PXC ProtocolLB Protocols – PXC Protocol

►Derived from PX protocol.Derived from PX protocol.►Exclusive locks are retained until end Exclusive locks are retained until end

of transaction (COMMIT or ROLLBACK).of transaction (COMMIT or ROLLBACK).►PXC helps to avoid loss of updates PXC helps to avoid loss of updates

bcause of ROLLBACK.bcause of ROLLBACK. No transaction is allowed to update an No transaction is allowed to update an

uncommitted changed record.uncommitted changed record.

Page 29: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols – PS / PSC LB Protocols – PS / PSC ProtocolsProtocols► Any transaction that updates a record must Any transaction that updates a record must

firstly ask for a shared lock of that record.firstly ask for a shared lock of that record.► During the transaction, just before the update During the transaction, just before the update

command, comes a request of changing the command, comes a request of changing the lock-S to lock-X. lock-S to lock-X.

► A transaction should not be allowed to lock A transaction should not be allowed to lock itself out.itself out.

► The goal here is to limit the duration of X-The goal here is to limit the duration of X-locks.locks.

Page 30: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols – PS ProtocolLB Protocols – PS Protocol

►Example (here deadlock occurs at T4):Example (here deadlock occurs at T4): Transaction ATransaction A Transaction Transaction

BB

SFIND Record1SFIND Record1 ----

---- ----

---- SFIND R1SFIND R1UPD Record1 --

---- ----

---- UPD R1UPD R1

TimeTime

T1T1

T2T2

T3T3

T4T4

Page 31: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols – PU / PUC LB Protocols – PU / PUC ProtocolProtocol

►This protocol uses a third lock state : the This protocol uses a third lock state : the update update locklock. .

►Any transaction that intends to update a record is Any transaction that intends to update a record is required to ask for U-lock of that record. A U-lock required to ask for U-lock of that record. A U-lock is compatible with an S-lock but not with another is compatible with an S-lock but not with another U-lock.U-lock.

►Replacing S-locks by U-locks will prevent deadlock.Replacing S-locks by U-locks will prevent deadlock.

Page 32: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols – PU / PUC LB Protocols – PU / PUC ProtocolProtocol

► Compatibility matrix :Compatibility matrix :

X S U

X False False False

S False True True

U False True False

►Example : compare PU and PS protocols

Page 33: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols – PU ProtocolLB Protocols – PU Protocol

►This protocol is more efficient than the This protocol is more efficient than the previous ones. previous ones.

► It limits considerably deadlock, It limits considerably deadlock, because it decreases the number of S-because it decreases the number of S-locks.locks.

Page 34: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols –Two Phase Locking LB Protocols –Two Phase Locking ProtocolProtocol

► 2PL 2PL ensures conflict-serializable schedules.ensures conflict-serializable schedules.► 2PL includes two phases :2PL includes two phases :

Growing phaseGrowing phase : transaction may obtain locks : transaction may obtain locks and may not release locksand may not release locks

Shrinking phaseShrinking phase : transaction may release : transaction may release locks and may not obtain locks.locks and may not obtain locks.

► The schedule is determined in the relation to The schedule is determined in the relation to the order of their the order of their lock pointslock points..

► If all transactions are two-phase, then all If all transactions are two-phase, then all executions are serializable.executions are serializable.

Page 35: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols –2PL ProtocolLB Protocols –2PL ProtocolExample :Example :

Page 36: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

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 . Here a transaction

must hold all its exclusive locks till it commits.must hold all its exclusive locks till it commits. Rigorous two-phase lockingRigorous two-phase locking is even stricter: is even stricter:

here allhere all locks (shared and exclusive) are held till locks (shared and exclusive) are held till commit. In this protocol transactions can be commit. In this protocol transactions can be serialized in the order in which they commit.serialized in the order in which they commit.

Graph-based protocol :Graph-based protocol : we fix an order of we fix an order of accessing data. If a transaction has to update accessing data. If a transaction has to update Row2 and read Row1, it has to access these data Row2 and read Row1, it has to access these data in a predefined order. in a predefined order.

Page 37: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols – Deadlock LB Protocols – Deadlock AvoidanceAvoidance►Deadlock preventionDeadlock prevention protocols ensure protocols ensure

that the system will never enter into a that the system will never enter into a deadlock state. It can be achieved deadlock state. It can be achieved using different strategies :using different strategies :

Transaction SchedulingTransaction Scheduling Request RejectionRequest Rejection Transaction RetryTransaction Retry

Page 38: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB-Protocols : Deadlock Prevention LB-Protocols : Deadlock Prevention StrategiesStrategies

►Timeout-Based SchemesTimeout-Based Schemes : : a transaction waits for a lock only for a a transaction waits for a lock only for a

specified amount of time. After that, the specified amount of time. After that, the wait times out and the transaction is wait times out and the transaction is rolled back.rolled back.

thus deadlocks are not possiblethus deadlocks are not possible simple to implement; but starvation is simple to implement; but starvation is

possible. Also difficult to determine good possible. Also difficult to determine good value of the timeout interval.value of the timeout interval.

Page 39: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB-Protocols : Deadlock Prevention LB-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 Some transactions will have to roll back to

break deadlock. Select that transaction as break deadlock. Select that transaction as victim that will incur minimum cost.victim that will incur minimum cost.

► We have to determine how far to roll back the We 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 Total rollback: Abort the transaction and then

restart it.restart it. Partial rollback: it is more effective to roll back Partial rollback: it is more effective to roll back

transaction only as far as necessary to break transaction only as far as necessary to break deadlock deadlock

Page 40: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Deadlock Avoidance : Transaction Deadlock Avoidance : Transaction SchedulingScheduling

►Two transactions will not be run Two transactions will not be run concurrently if their data requirements concurrently if their data requirements conflict.conflict.

►We We mustmust know what are the data know what are the data requirements of each transaction before requirements of each transaction before run time => impossible till runtime.run time => impossible till runtime.

►Consequently, the lock unit is a set of Consequently, the lock unit is a set of records and locks are applied at records and locks are applied at transaction initiation instead of during transaction initiation instead of during execution.execution.

Page 41: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Deadlock Avoidance : Request Deadlock Avoidance : Request RejectionRejection

►The system rejects any lock request The system rejects any lock request that cannot be applied.that cannot be applied.

► It uses the It uses the deadlock detection algorithmdeadlock detection algorithm..

►When trying to grant a lock request, if When trying to grant a lock request, if a deadlock is detected, the transaction a deadlock is detected, the transaction is rejected.is rejected.

Page 42: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Deadlock Avoidance : Transaction Deadlock Avoidance : Transaction RetryRetry► Transactions are timestamped with their start time.Transactions are timestamped with their start time.► Example : A requests a lock on a record already locked Example : A requests a lock on a record already locked

by B by B

► wait-diewait-die scheme — non-preemptive scheme — non-preemptive A waits if it is older than B, otherwise, it A waits if it is older than B, otherwise, it diesdies and it is rolled and it is rolled

back and automatically retried.back and automatically retried. a transaction may die several times before acquiring its a transaction may die several times before acquiring its

needed data itemneeded data item► wound-waitwound-wait scheme — preemptive scheme — preemptive

A waits if it is younger than B, otherwise it A waits if it is younger than B, otherwise it woundswounds (forces (forces rollback) of younger transaction instead of waiting for it. rollback) of younger transaction instead of waiting for it.

Younger transactions may Younger transactions may waitwait for older ones. for older ones. Less rollbacks than Less rollbacks than wait-diewait-die scheme. scheme.

► Transactions retain their timestamps even if they are Transactions retain their timestamps even if they are rolled back.rolled back.

Page 43: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB-Protocols : Deadlock Detection LB-Protocols : Deadlock Detection AlgorithmAlgorithm

►The system is in a deadlock state if The system is in a deadlock state if and only if and only if the wait-for graph has a the wait-for graph has a cycle. cycle.

►The system must invoke a deadlock-The system must invoke a deadlock-detection algorithm detection algorithm periodicallyperiodically to look to look for cycles.for cycles.

Page 44: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB-Protocols : Deadlock Detection LB-Protocols : Deadlock Detection AlgorithmAlgorithm

Wait-for graph without a cycle Wait-for graph with a cycle

Back

Page 45: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols : Locking LB Protocols : Locking GranularityGranularity

► Allow data items to be of various sizes and Allow data items to be of various sizes and define a hierarchy of data granularities, define a hierarchy of data granularities, where the small granularities are nested where the small granularities are nested within larger ones.within larger ones.

► Can be represented graphically as a tree. Can be represented graphically as a tree. When a transaction locks a node in the tree When a transaction locks a node in the tree explicitlyexplicitly, it , it implicitlyimplicitly locks all the node's locks all the node's descendents in the same mode.descendents in the same mode.

► Granularity of lockingGranularity of locking (level in tree where (level in tree where locking is done):locking is done): fine granularityfine granularity (lower in tree): high concurrency, (lower in tree): high concurrency,

high locking overheadhigh locking overhead coarse granularitycoarse granularity (higher in tree): low locking (higher in tree): low locking

overhead, low concurrencyoverhead, low concurrency

Page 46: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols : Locking LB Protocols : Locking GranularityGranularity

►The highest level in the example The highest level in the example hierarchy is the entire database.hierarchy is the entire database.

►The levels below are of type The levels below are of type areaarea, , filefile and and recordrecord in that order. in that order.

Page 47: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols : Intent Locking LB Protocols : Intent Locking ProtocolProtocol

► In addition to S and X lock modes, there are In addition to S and X lock modes, there are three additional lock modes with multiple three additional lock modes with multiple granularity: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 (IX): indicates explicit locking

at a lower level with exclusive or shared locksat a lower 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 rooted by that node is locked explicitly in shared mode and explicit locking is being done at a lower mode and explicit locking is being done at a lower level with exclusive-mode locks.level with exclusive-mode locks.

► Intention locks allow a higher level node to be Intention locks allow a higher level node to be locked in S or X mode without having to check locked in S or X mode without having to check all descendent nodes.all descendent nodes.

Page 48: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols : Intent Locking LB Protocols : Intent Locking ProtocolProtocol

IS IX S S IX X

IS

IX

S

S IX

X

Compatibility Compatibility Matrix :Matrix :

Page 49: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB Protocols : Intent Locking LB Protocols : Intent Locking ProtocolProtocol

► Transaction Transaction TTii can lock a node can 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 any 2. The root of the tree must be locked first, and may be locked in any

mode.mode. 3. A node 3. A node QQ can be locked by can be locked by TTii in S or IS mode only if the parent in S or IS mode only if the parent of of QQ is currently locked by is currently locked by TTii in either IX or IS mode. in either IX or IS mode. 4. A node 4. A node QQ can be locked by can be locked by TTii in X, SIX, or IX mode only if the in X, SIX, or IX mode only if the parent of parent of QQ is currently locked by is 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 node can 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 node can unlock a node QQ only if none of the children of only if none of the children of QQ are currently are currently

locked by locked by TTii..

► Locks are acquired in root-to-leaf order, whereas they are released in Locks are acquired in root-to-leaf order, whereas they are released in leaf-to-root order.leaf-to-root order.

Page 50: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

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 INSERT, UPDATE or DELETE will place a row row Exclusive LockExclusive Lock (X-lock). (X-lock).

SELECT...FROM...FOR UPDATE will place a SELECT...FROM...FOR UPDATE will place a row Shared Lock (S-lock).row Shared Lock (S-lock).

Page 51: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

LB ProtocolsLB Protocols

► Oracle Syntax:Oracle Syntax:LOCK TABLE [LOCK TABLE [schema.schema.] table [] table [optionsoptions] IN ] IN lock modelock mode MODE MODE

[NOWAIT] [NOWAIT] Options: Options:

► PARTITION partition PARTITION partition ► SUBPARTITION subpartition SUBPARTITION subpartition ► @dblink @dblink

Lock modes:Lock modes:► EXCLUSIVE EXCLUSIVE ► SHARE SHARE ► ROW EXCLUSIVE ROW EXCLUSIVE ► SHARE ROW EXCLUSIVESHARE ROW EXCLUSIVE► ROW SHARE* | SHARE UPDATE* ROW SHARE* | SHARE UPDATE*

Page 52: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Optimistic Concurrency Optimistic Concurrency ControlControl

ReadRead ValidationValidation WriteWrite

► A transaction in OCC is composed of three phases :A transaction in OCC is composed of three phases : Read PhaseRead PhaseTransactions 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 phaseFor 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 any conflict with another transaction. If there is, the transactiontransaction

is rolled back, otherwise the write phase can proceed.is rolled back, otherwise the write phase can proceed. Write phaseWrite phaseUpdates are written from the buffer to the database.Updates are written from the buffer to the database.

Page 53: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

CONCLUSIONCONCLUSION

►Locking is a pessimist concurrency Locking is a pessimist concurrency control, because it assumes maximum control, because it assumes maximum contention.contention.

►OCC is dead-lock free because it does OCC is dead-lock free because it does not implement locking.not implement locking.

Page 54: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

ReferencesReferences

► Date BookDate Book► Cooperative Cataloging in a Scalable Digital Library Cooperative Cataloging in a Scalable Digital Library

SystemSystem, Dr Hachim Haddouti, Dr Hachim Haddouti► Transaction ManagementTransaction Management, IBM Research , IBM Research

Laboratory, San Jose CaliforniaLaboratory, San Jose California► Performance of Performance of ConcurrencyConcurrency ControlControl Mechanisms Mechanisms

in Centralized Database Systemsin Centralized Database Systems, Vijay Kumar., Vijay Kumar.► Overview of Overview of concurrencyconcurrency controlcontrol and locking for and locking for

databasesdatabases. . www.odbmsfacts.comwww.odbmsfacts.com/articles/ /articles/

concurrency_control_and_locking.htmlconcurrency_control_and_locking.html

Page 55: Advanced Database Systems and Data Warehousing INTEGRITY AND CONCURRENCY IN DATABASE SYSTEMS By: Benmammass Mehdi.

Q&AQ&A

Integrity and Concurrency Integrity and Concurrency Control in Database Control in Database

SystemSystem