Professional Documents
Culture Documents
Bilal Dbms Unit-4
Bilal Dbms Unit-4
DB
MANAGEMENT
UNIT-4 M
Presented By
S Mohammad Bilal J
SYSTEM Assistant professor
Department I.T
VCE
UNIT-4
✓View Serializability
03 ✓ Serializability, 04
✓Testing for Serializability,
✓Conflict Serializability
T1 T2
A=1000
Read(A)
A=A+100
Write(A) Read(A)
B=A-100
Write(B)
Commit
Read(B)
Write(B)
Commit
Properties of Transaction (Acid Properties)
✓ To ensure consistency, completeness of the database in scenario of concurrent access, system failure, the following
ACID properties can be enforced on to database.
i . Atomicity
ii. Consistency
iii. Isolation
Iv. Durability
i. Atomicity
✓ This property states that either all of the instructions with in a transaction must be executed or none of them
should be executed.
✓ It involves following two operations.
i. Abort : If a transaction aborts, changes made to database are not visible.
ii. Commit : If a transaction commits, changes made are visible.
✓ Atomicity is also known as the ‘All or nothing rule’.
Example:
✓ Consider the following transaction T consisting of T1 and T2: Transfer of 100 from account X to account Y.
✓ Intermediate transaction results must be hidden from other concurrently executed transactions.
✓ If we execute a particular transaction in isolation (or) together with other transaction in multiprogramming
environment, the transaction should give same result in any case.
✓ When executing multiple transactions concurrently and trying to access shared resources the system should
create an order such that the only one transaction can access the shared resource at the same time & release it
after completion of it’s execution for other transaction.
✓ This property ensures that multiple transactions can occur concurrently without leading to inconsistency of
database state.
✓ Changes occurring in a particular transaction will not be visible to any other transaction until that particular change
in that transaction is written to memory or has been committed.
Note: To achieve isolation you should use locking mechanism among shared resources.
iii. Isolation
✓ example: Let X= 500, Y = 500. Consider two transactions T and T”.
✓ Suppose T has been executed till Read (Y) and then T’’ starts.
✓ As a result , interleaving of operations takes place due to which T’’ reads correct value of X but incorrect value of Y
and sum computed by T’’: (X+Y = 50, 000+500=50, 500) is thus not consistent with the sum at end of transaction: T:
(X+Y = 50, 000 + 450 = 50, 450).
✓ This results in database inconsistency, due to a loss of 50 units. Hence, transactions must take place in isolation
and changes should be visible only after a they have been made to the main memory.
✓ This property ensures that once the transaction has completed execution, the updates and modifications to the
database are stored in and written to disk and they persist even is system failure occurs.
✓ It is the responsibility of Recovery Manager.
Serializability
✓ Basic Assumption is serial execution of transactions preserves database consistency.
✓ Serializability is related to schedules and transactions.
✓ Schedule is a set of transactions, and a transaction is a set of instructions used to perform any logical operations in
terms of databases.
✓ A concurrent schedule is serializable if it is equivalent to a serial schedule.
✓ Serializability of schedules ensures that a non-serial schedule is equivalent to a serial schedule.
✓ It helps in maintaining the transactions to execute serially without interleaving one another.
✓ To test the serializability of a schedule, we can use Serialization Graph or Precedence Graph.
✓ A Precedence Graph is a Directed Graph of the entire transactions of a schedule.
✓ It can be defined as a Graph G(V, E) consisting of a set of directed-edges E = {E1, E2, E3, ..., En} and a set of vertices
V = {V1, V2, V3, ...,Vn}.
✓ The set of edges contains one of the two operations - READ, WRITE performed by a certain transaction.
Serializability
✓ If there is a cycle present in the graph then the schedule is non-serializable because the cycle indicates that one
transaction is dependent on the other transaction.
✓ On the other hand, no-cycle means that the non-serial schedule is serializable.
What is a conflicting pair in transactions?
✓ Two operations inside a schedule are called conflicting if they meet these three conditions:
✓ let’s take two transactions on data: "a". The conflicting pairs are:
T1 T2
READ(a) WRITE(a)
WRITE(a) WRITE(a)
WRITE(a) READ(a)
Serializability
Note: There can never be a read-read conflict as there is no change in the data.
Conflict Serializability
✓ A non-serial schedule is a conflict serializable if, after performing some swapping on the non-conflicting operation
results in a serial schedule.
✓ It is the process of checking the non-serial schedule and an equivalent serial schedule. This process of checking is
called Conflict Serializability.
✓ It is tedious to use if we have many operations and transactions as it requires a lot of swapping.
✓ First, we will check conflicting pairs operations(read-write, write-read, and write-write) and then form directed
edges between those conflicting pair transactions.
✓ If we can find a loop in the graph, then the schedule is non-conflicting serializable otherwise it is surely a
conflicting serializable schedule.
Conflict Serializability
Example-1: A schedule "S" having three transactions t1, t2, and t3 working simultaneously. Check Whether S is Conflict
Serializable or Not?
T1 T2 T3
R(x)
R(y)
R(y)
W(y)
W(x)
W(x)
R(x)
W(x)
✓ As there is no loop in the graph, it is a conflict serializable schedule as well as a serial schedule.
✓ Since it is a serial schedule, we can detect the order of transactions as well.
✓ The order of the Transactions: t1 will execute first as there is no incoming edge on T1. (i.e., In degree=0)
✓ T3 will execute second as it depends on T1 only.
✓ T2 will execute at last as it depends on both T1 and T3.
✓ So, order of its equivalent serial schedule is: T1 --> T3 --> T2
Conflict Serializability
Example-2: A schedule "S" having three transactions t1, t2, and t3 working simultaneously. Check Whether S is Conflict
Serializable or Not?
T1 T2 T3
R(x)
R(z)
W(z)
R(y)
R(y)
W(y)
W(x)
W(z)
W(x)
Step-1:
Step-2:
Step-3:
Step-1:
List all the conflicting operations and determine the dependency
between the transactions-
₋ R1(A) , W2(A) (T1 → T2)
₋ R2(A) , W1(A) (T2 → T1)
₋ W1(A) , W2(A) (T1 → T2)
₋ R1(B) , W2(B) (T1 → T2)
₋ R2(B) , W1(B) (T2 → T1)
Step-2:
Step-3:
✓ But, what if a schedule’s precedence graph contains a cycle/loop and is giving consistent result .
✓ So, to address such cases we brought the concept of View-Serializability because we did not want to confine only
to Conflict-Serializability.
✓ View Serializability is a process to find out that a given schedule is view serializable or not.
✓ To check whether a given schedule is view serializable, we need to check whether the given schedule is View
Equivalent or not.
View Serializability
View Equivalent:
✓ Lets learn how to check whether the two schedules are view equivalent.
✓ Two schedules T1 and T2 are said to be view equivalent, if they satisfy all the following conditions:
i. Initial Read
✓ Initial read of each data item in transactions must match in both schedules.
✓ For example, if transaction T1 reads a data item X before transaction T2 in schedule S1 then
✓ Read vs Initial Read: There may be confusion by the term initial read. Here initial read means the first read
operation on a data item, for example, a data item X can be read multiple times in a schedule but the first read
operation on X is called the initial read.
View Serializability
ii. Final Write:
✓ Final write operations on each data item must match in both the schedules.
✓ In S2, the last write operation on X should be performed by the transaction T1.
View Serializability
iii. Update Read:
✓ If in schedule S1, the transaction T1 is reading a data item updated by T2 then in schedule S2, T1 should read the
value after the write operation of T2 on same data item.
✓ For example, In schedule S1, T3 performs a read operation on A after the write operation on A by T2 then in S2, T3
should read the A after T2 performs write on A.
View Serializability
Problem-1 : Prove whether the given schedule is View-Serializable or not?
✓ = 3! = 6
S1 = <T1 T2 T3>
S2 = <T1 T3 T2>
S3 = <T2 T3 T1>
S4 = <T2 T1 T3>
S5 = <T3 T1 T2>
S6 = <T3 T2 T1>
View Serializability
Step 1: Initial Read S
✓ The initial read operation in S is done by T1 and in S1 it is also done by T1 .
✓ The final write operation in S is done by T3 and in S1, it is also done by T3.
✓ Lets check for Y. In schedule S1, transaction T1 first reads the data item Y. In
S2 also the first read operation on Y is performed by T1.
✓ We checked for both data items X & Y and the initial read condition is
satisfied in S1 & S2.
Step-2: Final Write
✓ In schedule S1, the final write operation on X is done by transaction T2. In S2 also transaction T2 performs the final
write on X.
✓ Lets check for Y. In schedule S1, the final write operation on Y is done by transaction T2. In schedule S2, final write
on Y is done by T2.
✓ We checked for both data items X & Y and the final write condition is satisfied in S1 & S2.
View Serializability
Step-3: Update Read:
✓ In S1, transaction T2 reads the value of X, written by T1. In S2, the same
transaction T2 reads the X after it is written by T1.
✓ In S1, transaction T2 reads the value of Y, written by T1. In S2, the same
transaction T2 reads the value of Y after it is updated by T1.
✓ The update read condition is also satisfied for both the schedules.
✓ Since all the three conditions are satisfied , which means S1 and S2 are view equivalent.
✓ Also, as we know that the schedule S2 is the serial schedule of S1, thus we can say that the schedule S1 is view
serializable schedule.
View Serializability
Method-2 :
✓ If the given schedule is non-conflict serializable, then it may or may not be view serializable. So we need to look at
the below cases.
✓ Blind write : Performing the Writing operation (updation), without reading operation, such write operation is
known as a blind write.
✓ If there exists any blind write, then, in that case, the schedule may or may not be view serializable.
✓ If it does not contain any blind-write, we can surely state that the schedule would not be View-Serializable.
Problem-3:Check whether the given schedule S is view serializable or not?
✓ So, let us check whether the given schedule is conflict serializable or not.
Step-01:
Now,
✓ Since, the given schedule S is not conflict serializable, so, it may or may
not be view serializable.
Step-01:
✓ List all the conflicting operations and determine the dependency between
the transactions-
Now,
✓ Since, the given schedule S is not conflict serializable, so, it may or may
not be view serializable.
✓ A schedule that can be successfully rolled back in case of any failure is known as recoverable Schedule.
✓ A schedule that cannot be rolled back because some transactions already used COMMIT to make the changes
permanent in database and these transactions have used values produced by the failed transactions. These types
of schedules are called irrecoverable schedules.
i.Irrecoverable Schedules
A transaction performs a dirty read operation from an uncommitted transaction and commits before the transaction
from which it has read the value then such a schedule is known as an Irrecoverable Schedule.
Example:
Consider the following schedule
Here,
✓ T2 performs a dirty read operation.
✓ T2 commits before T1.
✓ T1 fails later and roll backs.
✓ The value that T2 read now stands to be incorrect.
✓ T2 can not recover since it has already committed.
ii. Recoverable Schedules
✓ A transaction performs a dirty read operation from an uncommitted transaction and its commit operation is
delayed till the uncommitted transaction either commits or roll backs then such a schedule is known as a
Recoverable Schedule.
✓ The commit operation of the transaction that performs the dirty read is delayed.
✓ This ensures that it still has a chance to recover if the uncommitted transaction fails later.
Example-:
✓ T1 commits later.
Method-01:
Method-02:
✓ Check if there exists any dirty read operation.(Reading from an uncommitted transaction is called as a dirty read)
✓ If there does not exist any dirty read operation, then the schedule is surely recoverable.
✓ If there exists any dirty read operation, then the schedule may or may not be recoverable.
✓ If there exists a dirty read operation, then follow the following cases:
Recoverability
Types of Recoverable Schedules:
i. Cascading Schedule
✓ If in a schedule, failure of one transaction causes several other dependent transactions to rollback or abort, then such a schedule
is called as a Cascading Schedule or Cascading Rollback or Cascading Abort.
Example:
In this schedule,
NOTE-If the transactions T2, T3 and T4 would have committed before the failure of transaction T1, then the schedule would have
been irrecoverable.
ii. Cascadeless Schedule
✓ If in a schedule, a transaction is not allowed to read a data item until the last transaction that has written it is
committed or aborted, then such a schedule is called as a Cascade-less Schedule.
Example:
✓ A lock is kind of a mechanism that ensures that the integrity of data is maintained. It does that, by locking the data
while a transaction is running, any other transaction cannot read or write the data until it acquires the appropriate
lock.
✓ Types of Locks:
i. Binary locks
Binary Locks :
✓ which are often denoted as lock-S(), is defined as locks that provide Read-Only access to the information associated
with them.
✓ Whenever a shared lock is used on a database, it can be read by several users, but these users who are reading the
the data items will not have permission to edit it or make any changes to the data items.
Exclusive Lock:
✓ This lock allows both the read and write operation, Once this lock is placed on the data no other lock (shared or
Exclusive) can be placed on the data until Exclusive lock is released.
✓ Lock Compatibility Matrix controls whether multiple transactions can acquire locks on the same resource at the
same time.
✓ If a resource is already locked by another transaction, then a new lock request can be granted only if the mode of
the requested lock is compatible with the mode of the existing lock.
✓ Any number of transactions can hold shared locks on an item, but if any transaction holds an exclusive lock on
item, no other transaction may hold any lock on the item.
Concurrency Control
Upgrading And Downgrading of Locks:
✓ If a transaction is holding an exclusive lock over an object .It can simply downgrade from exclusive lock to shared
lock after completion of its updation.
✓ Similarly a shared lock can be upgraded to exclusive lock on particular data item. when there is no other
transaction is holding exclusive lock on same data item
Locking Protocols
✓ Locking protocols are 2-types,they are:
✓ It allow transactions to obtain a lock on every object before a 'write' operation is performed.
✓ Transactions may unlock the data item after completing the ‘write’ operation.
A. Deadlocks
B. Starvation
2-Phase Locking Protocol
✓ This locking protocol divides the execution phase of a transaction into two
parts.
✓ The First part is where the transaction acquires all the locks.
✓ As soon as the transaction releases its first lock, the second phase starts. In
this phase the transaction cannot demand any new locks; it only releases the
acquired locks.
✓ The Point at which the growing phase ends, i.e., when a transaction takes the final lock it needs to carry on its
work
✓ Before initiating an execution, the transaction requests the system for all the locks it needs.
✓ If all the locks are granted, the transaction executes and releases all the locks when all its operations are over.
✓ If all the locks are not granted, the transaction rolls back and waits until all the locks are granted.
✓ Since in the schedule the lock on a is released by T1, it is possible for the
transaction T2 to acquire the lock on a.
✓ It mean that even though a conservative two phase protocol is used, the
schedules may not necesssarily be cascadeless.
ii. Strict Two – Phase Locking Protocol
✓ In this method all Exclusive(X) locks held by the transaction be
released after the Transaction Commits.
✓ In second phase Strict-2PL does not release a lock after using it. It
releases only all exclusive locks after a transaction is committed.
✓ This protocol requires that all the share and exclusive locks to be held until
the transaction commits.
✓ It releases all the locks including shared and exclusive locks after committing
the transactions.
✓ Same as Strict 2PL but Hold all locks until the transaction has already
successfully committed or aborted.
✓ There is no deadlock.
Locking Protocols
Cascading Roll Back in 2PL:
S no. T1 T2 T3
✓ Because of Write(R1) in T1 and after that, Read(R1) in T2 and
1 Lock-X(R1)
Read(R1) in T3, there is a dirty read which means the data written 2 Read(R1)
1 Lock-X(R1) Lock-X(R2)
2 Read(R1) Read(R2)
3 Lock-X(R2) Lock-X(R1)
Deadlock in 2PL:
✓ In the above transaction in step 3, T1 cannot execute the exclusive lock in R2 as the Exclusive lock is already
obtained by transaction T2 in step 1.
✓ Also, in step 3, the Exclusive lock on R1 cannot be executed by T2 as T1 has already applied the Exclusive lock on
R1.
✓ So, if we draw a wait-for graph, we can see that T1 waits for R2 and T2 waits for R1, which creates conflict, and the
waiting time never ends, which leads to a deadlock.
Timestamp based Ordering Protocol
✓ Timestamp ordering protocol maintains the order of transaction based on their timestamps.
✓ A timestamp is a unique identifier that is being created by the DBMS when a transaction enters into the system.
This timestamp can be based on the system clock or a logical counter maintained in the system.
✓ Timestamp helps identifying the older transactions and gives them higher priority compared to the newer
transactions. This make sure that none of the transactions are pending for a longer period of time.
✓ This protocol also maintains the timestamps for the last read and last write on a data.
Example:
✓ let’s say an old transaction T1 timestamp is TS(T1) and a new transaction T2 enters into the system, timestamp
assigned to T2 is TS(T2).
✓ Here TS(T1) < TS(T2) so the T1 has the higher priority because its timestamp is less than timestamp of T2.
Timestamp based Ordering Protocol
Read time-stamp of data-item:
✓ It is denoted as R_TS(A)
✓ R_TS(A) is the largest or youngest timestamp of a transaction that executed the operation read(A)
successfully.
✓ It is denoted as W_TS(A) .
✓ W_TS(A) is the largest or youngest timestamp of a transaction that executed the operation write(A)
successfully.
Timestamp based Ordering Protocol
Basic Timestamp ordering protocol works as follows:
1. Check the following condition whenever a transaction Ti issues a Read (X) operation:
✓ If TS(Ti) < W_TS(X) then the operation is rejected and Ti is rolled back otherwise the operation is
executed.
Timestamp based Ordering Protocol
Whenever a Transaction Tn issues a Read(A) operation, this
protocol checks the following conditions:
Whenever a Transaction Tn issues a Write(A) operation,
If ( W_TS(A) > TS(Tn) ) this protocol checks the following conditions:
{
abort and reject Tn If ( R_TS(A) > TS(Tn) or W_TS(A) > TS(Tn) )
}
else {
{ abort and rollback the transaction Tn
Read(A) operation of Tn
update the timestamp R_TS(A) }
} else
{
Write(A) operation of Tn
update W_TS(A) to TS(Tn)
}
Timestamp based Ordering Protocol
Advantages of Timestamp based protocol:
✓ Schedules managed using timestamp based protocol are serializable just like the two phase protocols
✓ Since older transactions are given priority which means no transaction has to wait for longer period of time that
makes this protocol free from deadlock.
✓ This rule states if TS(Ti) < W_TS(A), then the operation is rejected and Ti is rolled back.
✓ Time-stamp ordering rules can be modified to make the schedule view serializable. Instead of making Ti rolled
back, the 'write' operation itself is ignored and continue the remaining operations.
‘U’ T
K H
N A