Sppu dbms unit 5
Sppu dbms unit 5
5 Database Transaction
Management
Syllabus
Introduction to Database Transaction, Transaction states, ACID properties, Concept of Schedule,
Serial Schedule. Serializability : Conflict and View, Cascaded Aborts, Recoverable and Non
recoverable Schedules. Concurrency Control : Lock-based, Time-stamp based Deadlock handling.
Recovery methods : Shadow-Paging and Log-Based Recovery, Checkpoints. Log-Based Recovery :
Deferred Database Modifications and Immediate Database Modifications.
Contents
Part I : Transaction Management
5.1 Introduction to Database Transaction
5.2 Transaction states Dec 17, .Marks 8
5.3 ACID Properties .Dec.-18,19, May-18,19, ..... Marks 8
5.4 Concept of Schedule
5.5 Serializability : Conflict and View... .Dec.-17,18,19, May-18, ..Marks 9
5.6 Recoverable and Non-Recoverable Schedules
(5-1)
Database Management Systems 5-2 Database Transaction Management
Step 4:Store the updated block from the buffer back to disk.
TECHNICAL PUBLICATIONS. an up- thrust for knowledge
Database Transaction Management
Database Management Systems 5-3
write operations
Example of sample transaction performing read and
read item(X);
X=X+M;
Writeitem(X);
" Transaction Notations :
write operations. For example -
The transaction notations focuses on read and
Following are two transactions denoted by Tland T2
T1:bl;r1(X);w1(X);r1(Y); W1(Y);e1;
T2:b2;r2(X);r2(Y);e2
bl and b2 represents the
The r and w represents the read and write operations. The
beginning and el and e2 represents ending of transaction.
SPPU : Dec..-17. Marks 8
5.2 Transaction States
Each transaction has following five states:
Partially Committed
committed,
Active End
Failed Aborted
1) Active : This is the first state of transaction. For example : Insertion, deletion or
updation of record is done here. But data is not saved to database.
2) Partially committed : When atransaction executes its final operation, it is said to be
in a partially committed state.
3) Failed :Atransaction is said to be in a failed state if any of the checks made by the
database recovery system fails. A failed transaction can no longer proceed further.
4) Aborted : If a transaction is failed to execute, then the database recovery system will
make sure that the database is in its previous consistent state. If not, it brings the
database to consistent state by aborting or rolling back the transaction.
5) Committed : If a transaction executes all its operations successfully, it is said to be
committed. This is the last step of a transaction, if it executes without fail.
1) Atomicity :
" This property states that each transaction must be considered as a single unit and
must be completed fully or not completed at all.
" No transaction in the database is left half completed.
" Database should be in a state either before the transaction execution or after the
transaction execution. It should not be in a state 'executing.
" For example - In above mentioned withdrawal of money transaction all the five
steps must be completed fully or none of the step is completed. Suppose if
transaction gets failed after step 3, then the customer will get the money but the
balance willnot be updated accordingly. The state of database should be either at
before ATM withdrawal (i.e customer without withdrawn money) or after ATM
withdrawal (i.e. customer with money and account updated). This will make the
system in consistent state.
2) Consistency :
" The database must remain in consistent state after performing any transaction.
" For example : In ATM withdrawal operation, the balance must be updated
appropriately after performing transaction. Thus the database can be in consistent
state.
3) Isolation :
" In a database system where more than one transaction are being executed
simultaneously and in parallel, the property of isolation states that all the
transactions will be carried out and executed as if it is the only transaction in the
system.
" No transaction will affect the existence of any other transaction.
" For example : If a bank manager is checking the account balance of particular
customer, then manager should see the balance either before withdrawing the
money or after withdrawing the money. This will make sure that each individual
transaction is completed and any other dependent transaction will get the consistent
data out of it. Any failure to any transaction will not affect other transaction in this
case. Hence it makes all the transactions consistent.
4) Durability :
" The database should be strong enough to handle any system failure.
" If there is any set of insert /update, then it should be able to handle and commit to
the database.
" If there is any failure, the database should be able to recover it to the consistent
state.
" For example : In ATM withdrawal example, if the system failure happens after
customer getting the money then the system should be strong enough to update
Database with his new balance, after system recovers. For that purpose the system
has to keep the log of each transaction and its failure. So when the system
recovers, it should be able to know when a system has failed and if there is any
pending transaction, then it should be updated to Database.
Review Question
1 State and explain in brief the ACID properties. During execution of transaction, a transaction
passes through several states, until it finally commits or aborts. List all possible sequences of
states through which a transaction may pass. Explain why each state transition occurs.
SPPU: May-18,19, Dec.-18,19, End Sem, Marks 8
5.5 Serializability : Conflict and View SPPU: Dec.-17.18,19. May-18, Marks 9
" When multiple transactions run concurrently, then it may lead to inconsistency of
data (i.e. change in the resultant value of data from different transactions).
Serializability is a concept that helps to identify which non serial schedule and find
the transaction equivalent to serial schedule.,
T1 A B T2
A=A - 10
W(A)
B=B+10
W(B)
90 110
A=A-10
W(A)
110
In above transactions initially T1 will read the values from database as A = 100,
B= 100 and modify the values of A and B. But transaction T2 will read the modified
value i.e. 90 and will modify it to 80 and perform write operation. Thus at the end of
transaction T1 value of A will be 90 but at end of transaction T2 value of A will be
80. Thus conflicts or inconsistency occurs here. This sequence can be converted to a
sequence which may give us consistent result. This process is called serializability.
Difference between serial schedule and serializable schedule
Sr. Serial schedule Serializable schedule
No.
2 In serial schedule, if there are two transactions In serializable schedule, if there are two
executing at the same time and no interleaving transactions executing at the same time and
of operations is permitted, then following can interleaving of operations is allowed there
be the possibilities of execution - can be different possible orders of executing
i) Execute all the operations of transactions T, an individual operation of the transactions.
in a sequence and then execute all the
operations of transactions T, in a sequence.
ii) Execute all the operations of transactions T,
in a sequence and then execute all the
operations of transactions T, in a sequence.
Read(A) Read(A)
A=A-50 A=A-50
Write(A) Write(A)
Read(B) Read(B)
B=B+100 B=B+100
Write(B) Write(B)
Read(A) Read(B)
A=A+10 Write(B)
Write(A)
There are two types of serializabilities : Conflict serializability and view
serializability.
5.5.1 Conflict Serializability
Definition : Suppose T, and T, are two transactions and I, and I, are the instructions in
T, and T, respectively. Then these two transactions are said to be conflict serializable, if
both the instruction access the data item d, and at least one of the instruction is write
operation.
What is conflict ? : In the definition three conditions are specified for a conflict in
conflict serializability -
1) There should be different transactions
2) The operations must be performed on same data items
3) One of the operation must be the Write (W) operation.
" We can test a given schedule for conflict serializability by constructing a precedence
graph for the schedule, and by searching for absence of cycles in the graph.
Predence graph is a directed graph, consisting of G = (V,E) where V is set of vertices
and E is set of edges. The set of vertices consists of all the transactions participating
in the schedule. The set of edges consists of all edges T, ’I, for which one of three
conditions holds :
1 I, executes write() before T, executes read(Q).
" A serializability order of the transactions can be obtained by finding a linear order
consistent with the partial order of the precedence graph. This process is called
topological sorting.
Testing for serializability
Following method is used for testing the serializability: To test the conflict
serializability we can draw a graph G = (V, E) where V = Vertices which represent
the number of transactions.
Step 2: Find the conflicting pairs (RW, WR, WW) on the same variable (or data item) by
different transactions.
Step 3: Draw edge for the given schedule. Consider following cases
1. T, executes write(Q) before T, executes read(@), then draw edge from T, to T,.
2. TË executes read(Q) before T;executes write(Q), then draw edge from T, to T,
3. T; executes write(Q) before T; executes write(Q). . then draw edge from T, to T,.
R(A)
W(A)
R(A)
R(B)
R(B)
W(B)
5.5.2 View Serializability
" If a given schedule is found to be view equivalent to some serial schedule, then it is
called as a view serializable schedule.
View Equivalent Schedule : Consider two schedules S1 and $2 consisting of
transactions T1 and T2 respectively, then schedules S1 and S2 are said to be view
equivalernt schedule if it satisfies following three conditions :
o If transaction Tl reads a data item A from the database initially in schedule S2,
then in schedule S2 also, T1 must perform the initial read of the data item X from
the database. This is same for all the data items. In other words - the initial reads
must be same for all data items.
o If data item A has been updated at last by transaction Ti in schedule S1, then in
schedule S2 also, the data item A must be updated at last by transaction Ti.
o If transaction Ti reads a data item that has been updated by the transaction TË in
schedule S1, then in schedule S2 also, transaction Ti must read the same data item
that has been updated by transaction TË. In other words the Write-Read sequence
must be same.
Step 2: If it is not conflict serializable schedule then check whether there exist any blind
write operation. The blind write operation is a write operation without reading a value. If
there does not exist any blind write then that means the given schedule is not view
serializable. In other words if a blind write exists then that means schedule may or may
not be view conflict.
Step 3: Find the view equivalence schedule
Example 5.5.7 Consider the following schedules for checking if these are view serializable or
n0t.
W(C)
R(A)
W(B) R(B)
R(C)
W(B)
W(B)
Solution:
Shared lock
Exclusive lock
S T F
X F F
W(A)
Unlock(A)
Lock-S(A)
R(A)
Unlock(A)
For example -
Lock(A) Lock point
Lock(B) Locks
Lock(C) get Locks are
acquired released
(Growing (Shrinking
Lock phase) phase)
Point!
Read(A) Read(B)
A=A-50 Unlock-S(B)
Write(A)
Lock-X(B)
Unlock-X(A)
B=B+100 Lock-S(A)
Write(B) Read(A)
Unlock-X(B) Unlock-S(A)
The important rule for being a two phase locking is -All lock operations precede all
the unlock operations.
In above transactions T, is in two phase locking mode but transaction T, is not in two
phase locking. Because in T,, the shared lock is acquired by data item B, then data item B
is read and then the lock is released. Again the lock is acquired by data item A, then the
data item A is read and the lock is then released. Thus we get lock-unlock-lock-unlock
sequence. Clearly this is not possible in two phase locking.
T,
W(A)
R(A)
Unlock(A)
Lock-S(A)
R(A)
Unlock-S(A)
Thus only after commit operation in T, we can unlock the exclusive lock. This ensures
the strict serializability.
Thus compared to basic two phase locking protocol, the advantage of strict 2PL
protocol is it ensures strict serializability.
2) Rigorous two phase locking : This is stricter two phase locking protocol. Here all
locks are to be held until the transaction commits. The transactions can be
seriealized in the order in which they commit.
Example - Consider transactions
R(A)
R(B)
W(B)
Unlock(A)
Unlock(B)
Thus the above transaction uses rigorous two phase locking mechanism.
5.10 Time-stamp based Protocol SPPU: May-19, Dec.-17, Marks 9
The time stamp ordering protocol is ascheme in which the order of transaction is
decided in advance based on their timestamps. Thus the schedules are serialized
according to their timestamps.
The timestamp-ordering protocol ensures that any conflicting read and write
operations are executed in timestamp order.
A larger timestamp indicates a more recent transaction or it is also called as
younger transaction while lesser timestamp indicates older transaction.
Assume a collection of data items that are accessed, with read and write operations,
by transactions.
" For each data item X the DBMS maintains the following values:
o RTS(X): The timestamp on which object X was last read (by some transaction
T,, ie., RTS(X):=TS(T) (Note that : RTS stands for Read Time Stamp]
o WTS(X) : The timestamp on which object X was last written (by some
transaction I,, ie, WTS(X):=TS(T)) (Note that : WTS stands for Write Time
Stamp]
For the following algorithms we use the following assumptions : A data item X in
the database has a RTS() and WTS(X). These are actually the timestamps of read
and write operations performed on data item Xat latest time.
A transaction T attempts to perform some action (read or write) on data item Xon
some timestamp and we call that timestamp as TS(T).
By timestamp ordering algorithm we need to decide whether transaction T has to be
aborted or T can continue execution.
R(X)
W(X)>
This read operation
gets rejected as it
occurs atthan
timestamp olderthe
write operation
Fig. 5.10.1
Database Management Systems 5-44 Database Transaction Management
ii)Suppose we have two transactions T, and T, with timestamps 10 sec. and 20 sec.
respectively.
10 Sec 20 Sec
T, T,
W()
ROX)
T, T,
R(X)
W(X)
W(X)
10 Sec 20 Sec
T, Tz
R(X)
W(X)
ii) Suppose we have two transactions T, and T, with timestamps 10 sec. and 20 sec.
respectively.
10 Sec 20 Sec
T, T,
W(X)
W(X)
RTS() and WTS(X) is initially -0
Then WTS(X) = 10 as transaction T, executes.
Now if write operation W(X) occurs on transaction T, at TS(T,) = 20 then
TS(T,) i.e. 20 >WTS(X) which is 10, hence we accept write operation on T,. The
transaction T, will perform write operation and now WTS will be updated as,
WIS(X) = 20
Advantages and disadvantages of time stamp ordering
Advantages
1) Schedules are serializable
2) No waiting for transaction and hence there is no deadlock situation.
Disadvantages
1) Schedules are not recoverable once transactions occur.
2) Same transaction may be continuously aborted or restarted.
Database Management Systems 5- 57 Database Transaction Management
Fig. 5.16.1
The log must be maintained on the stable storage and the entries in the log file are
maintained before actually updating the physical database.
There are two approaches used for log based recovery technique - Deferred
Database Modification and Immediate Database Modification.