Chapter18 1
Chapter18 1
Chapter18 1
Concurrency
Control
Sukarna Barua
Associate Professor, CSE, BUET
03/20/2024
Concurrency Control
03/20/2024
Control
03/20/2024
Role of Scheduler
Every request for read and write of database elements are passed to scheduler.
Two cases arise:
Case 1: Schedule execute read/write
immediately when it is safe to do so using
memory buffers.
Case 2: Scheduler delays the read/write as
it is not safe due to other transactions
accessing the same elements and can
cause inconsistent database state.
03/20/2024
Schedule
A schedule is a sequence of database actions
Actions come from one ore more transactions.
Database actions are same as the actions taken by a transaction.
Database actions of the transactions occur in the order as determined by a schedule
Purpose of schedule is safe execution of concurrent transactions.
Some assumptions
Read and write take place in main memory buffers.
A database element brought to a buffer by some transaction T may be read or
written not only by T but by other transactions.
03/20/2024
Schedule
Serial Schedule
03/20/2024
Serial Schedule
03/20/2024
Serial Schedule
Consider the two serial schedules. Assume initially A = B = 1000. Are the values of A
and B same at the end of the two schedules?
NO! Final values of database elements are dependent on the order of transactions.
03/20/2024
Serializable Schedule
03/20/2024
Serializable Schedule
03/20/2024
Serializable Schedule
03/20/2024
Serializable Schedule
DBMS schedule only takes into account the READ and WRITE requests and
determine a schedule that is serializable.
For DBMS scheduler, a transaction is represented by following actions:
: Transaction T reads database element .
: Transaction T writes database element .
03/20/2024
03/20/2024
Conflict Serializable Schedule
03/20/2024
Conflicts
Assume and are different transactions. The following pair of actions are not
conflicting:
03/20/2024
Conflicts
always conflict.
- The order of actions in a transaction are fixed and cannot be changed.
always conflict.
- Two writes of the same element by different transactions conflict.
- Swapping order changes the final value of .
always conflict.
- A read and a write of the same element by different transactions conflicts.
- Swapping order changes the value read by .
03/20/2024
Conflicts
Summarize:
Any two actions of different transactions may be swapped unless
- They involve same database element, and
- At least one is a write.
03/20/2024
Conflict Serializable
Conflict-equivalent: Two schedules are conflict equivalent if one schedule can be
turned into other schedule by a sequence of nonconflicting swaps of adjacent actions.
- Swapping only adjacent actions are allowed.
03/20/2024
Conflict Serializable
A conflict-serializable schedule is also a serializable schedule.
Conflict-serializability is a sufficient condition for serializability.
03/20/2024
Conflict Serializable
Consider the schedule below. Is it conflict-serializable?
03/20/2024
Test for Conflict-serializability
Precedence: Given a schedule S, involving transactions and , we say takes
precedence over , written if there are actions of and of such that:
- is ahead of in S,
- Both and involve same database element, and
- At least one of and is a write action.
03/20/2024
Test for Conflict-serializability
Precedence graph:
All precedence's between transactions can be summarized using a graph.
- Nodes are transactions of S.
- There is a directed edge from to if .
Known as precedence graph.
03/20/2024
Test for Conflict-serializability
Construct the precedence graph for schedule S.
If there is a cycle in the precedence graph, then the schedule is not conflict-
serializable.
For the shown precedence graph below, a conflict-equivalent serial schedule is (T1,
T2, T3).
03/20/2024
Test for Conflict-serializability
Consider the following schedule.
03/20/2024
Why Precedence Graph Test Work
Show that if there is a cycle in precedence graph, then the schedule is not conflict-
serializable.
03/20/2024
Why Precedence Graph Test Work
Converse: Show that if there is no cycle in precedence graph, then the schedule is
conflict-serializable.
Induction step: Assume the statement is true for a schedule involving transactions.
We show that it is also true for schedule involving transactions.
03/20/2024
Why Precedence Graph Test Work
Induction step:
Consider the schedule involving transactions and its precedence graph that has no
cycle.
As the graph is acrylic, there is a node which does not have any arc in. Let the
node be and corresponding transaction .
Since there are no arcs into node no action in any other transaction conflicts with
actions of . [why?]
Hence, all actions of can be moved to the front of the schedule by swapping with
other interleaving actions.
This results in the following schedule:
03/20/2024
Why Precedence Graph Test Work
Induction step:
…
03/20/2024
Enforcing Serializability by Locks
Rules for appropriate uses of locking: Following two rules must be preserved.
03/20/2024
Enforcing Serializability by Locks
New actions for locking and unlocking by transactions:
- Transaction request a lock on
- : Transaction releases its lock on
03/20/2024
Enforcing Serializability by Locks
Consider the schedule with lock instructions added.
Each transactions is consistent according to lock principle. [ why? ]
The schedule is legal according to lock principle. [ why? ]
03/20/2024
Locking scheduler
Locking schedule grants locks if and only if the request will result in a legal schedule.
03/20/2024
Locking scheduler
Consider the following schedule.
Note why lock is denied for [ T2 is kept waiting for the lock ]
03/20/2024
Locking and Conflict Serializability: Two-
phase locking
Two-phase locking:
Condition: In every transaction, all locks precede all unlock actions.
03/20/2024
Two-phase locking
Example of 2PL: The following schedule satisfies 2PL condition. [ why? ]
03/20/2024
2PL Schedule Is Conflict
Serializable
Prove that a schedule satisfying 2PL condition is conflict serializable.
Proof by induction:
For , this is trivial [a schedule involving a single transaction is always conflict
serializable]
Induction hypothesis: Assume, a schedule of transactions satisfying 2PL conditions is
conflict serializable.
We have to show that statement is true for .
03/20/2024
2PL Schedule Is Conflict
Induction step:
Serializable
Consider .
Consider the first unlock action in schedule . This action is from transaction .
There is no unlock action preceding . [ why? ]
No actions of any other transaction can conflict with actions of . [ why? ]
As per 2PL, is holding all locks, and thus no other transaction has locks on
elements accessed by
Thus, actions of can be moved forward to the front of the schedule.
We get a new schedule as follows:
03/20/2024
2PL Schedule Is Conflict
Induction step:
Serializable
…
[Proof is complete]
03/20/2024
2PL: Potential for Deadlock
Consider the schedule below that satisfying 2PL [no unlock before locks].
At this position, no transactions can proceed as one is waiting for the other by the
locking schedule.
This condition is known as deadlock!
03/20/2024