Professional Documents
Culture Documents
Transaction Management
Transaction Management
Transaction Management
Transaction refers to a collection of operations that form a single logical unit of work.
e.g.- Transfer of money from one account to another is a transaction which consists of two
updates, one to each account.
1. read A
2. A : = A – 50
3: write (A)
4. read (B)
5. B : = B + 50
6. write (B)
ACID properties:
A Atomicity, C Consistency, I Isolation, D Durability
Atomicity:
Either all operations of the transaction are properly reflected in the database or none.
Consistency:
It involves beginning a transaction with a consistent database and finishing with a consistent
database.
In between, database may be temporarily inconsistent.
When the transaction completes successfully, the database must be in consistent set.
Isolation:
Although multiple transaction may execute concurrently each transaction must be unaware of
other concurrently executing transactions. Intermediate transaction results must be hidden
from other concurrently executed transactions.
Durability:
After a transaction completes successfully, the changes it has made to the database persists
even if there is a system failure.
Termination
Active:
The initial state when the transaction has started execution.
Partially Committed:
At any given point of time, if the transaction is executing property, then it is going towards its
COMMIT point.
Committed:
The values generated during the execution are all stored in a volatile storage. If the transaction
fails for some reason, the temporary values are no longer required and the transaction is set
the roll back.
It means that any change made to the database by this transaction up to the point of the failure
must be undone.
After the transaction has been rolled back, the database restores its state prior to the
transaction.
COMMIT:
If no failure occurs, then the transaction reaches the COMMIT point. All the temporary values
are written to the stable storage and transaction has said to be committed.
Terminated:
Either committed or aborted, the Transaction finally reaches the terminated state.
Handling of Atomicity, Durability:
Shadow Database Scheme:
Before Update
During Update
After Update
Serial:
T1
Read A,
A = A – 100;
Write A
Read B
B = B + 100;
Write B
T2
Read A
Temp = A*0.1
Read C
C = C + Temp;
Write C;
In concurrent schedule CPU time is shared among two or more transactions in order to run
them concurrently. However, this creates a possibility that more than one transaction may
need to access a single data item for read or write purpose and the database could contain
inconsistent values if such accesses are not handled properly.
Concurrent:
T1 T2
Read A,
A = A – 100;
Write A
Read A
Temp = A*0.1
Read C
C = C + Temp;
Write C;
Read B
B = B + 100;
Write B;
Serializability:
When several concurrent transactions are trying to access the same data item the instructions
within this concurrent transactions, must be ordered in some ways so as there are no problem
in accessing or releasing shared data-item.
Conflict Serializability:
Two instructions of two different transactions may want to access the same data item in order
to perform a read or write operation.
Conflict Serializability deals with detecting whether the instructions are conflicting in any way
and specifying the orders in which these two instructions will be executed in case there is any
conflict.
A conflict arises if at least one or both of the instructions is a right operation.
Read(A) Read(A)
Write(A) Write(A)
Read(B) Read(B)
Read(A) Write(B)
Write(B) Read(A)
Write(A) Write(A)
Read(B) Read(B)
Write(B) Write(B)
T3 T4
Read(Q)
Write(Q)
Write(Q)
**Swapping can’t be done as ordering is important here. So this is not conflict serializable.
View Serializability:
Let S and S’ be two schedules with the same set of transactions. S & S’ are view equivalent if the
following three conditions are made for each data item Q.
i) If in schedule S transaction Ti reads the initial value of Q, then in schedule S’ also
transaction Ti must read the initial value of Q.
ii) If in schedule S, transaction Ti read Q and that value was produced by a write(Q) and
that value is produced by a read(Q) executed by a transaction Tj, then in schedule S’
also transaction Ti must read the value of Q, that was produced by the same write(Q)
operation transaction Tj.
iii) The transaction that performs the final write(Q) operation in schedule S, must also
perform the final write(Q) operation in schedule S’.
A schedule S is view Serializable, if it is view equivalent to a serial schedule.
View Equivalent
S S’
T3 T4 T6 T3 T4 T6
Read(Q)
Read(Q)
Write(Q)
Write(Q)
Write(Q)
Write(Q)
Write(Q)
Write(Q)
1) Conflict equivalent
2) View equivalent
3) Result equivalent (but not conflict equivalent)
4) Recoverable Schedule
5) Cascadeless Schedule
** (4) and (5) are applicable for transaction failures during concurrent execution.
T1 T5 T1 T5
Read(A)
A = A – 50 Read(A)
Write(A) A = A – 50
Read(B) Write(A)
B = B + 50 Read(B)
Write(B) B = B – 10
Read(B) Write(B)
B = B – 10 Read(B)
Write(B) B = B + 50
Read(A) Write(B)
A = A + 10 Read(A)
Write(A) A = A + 10
Write(A)
A Recoverable Schedule is one where for each pair of transaction Ti & Tj, Tj reads a data-
item previously written by Ti, the commit operation of Ti appears before the commit
operation of Tj.
T8 T9
Read(A)
Write(A)
Read(A)
Read(B)
Commit
Commit
1) T8 fails.
2) T9 commits immediately after Read(A). T9 has read the value of data item A, written by
T8.
We must abort T9 to ensure the atomicity of transaction. However, T9 is already
committed, can’t be aborted. So it is impossible to recover the situation.
So commit operation of T8 should occur before the commit operation of T9.
Cascadeless Schedule:
To recover correctly from the failure of a transaction Ti in a schedule we may have to roll back
several transactions. Such situations occur if transactions has resd data written by Ti.
T10 T11 T12
Read(A)
Read(B)
Write(A)
Commit Read(A)
Write(A)
Commit Read(A)
Commit
Precedence Graph:
A precedence graph is a directed graph G = (N, E) where N = {T1, T2, . . ., Tn} is a set of nodes
and E = {e1, e2, . . ., en} is a set of directed edges. For each transaction Ti in a schedule,
there exists a node in the graph. The edge ei (connecting Ti Tj ) is created if one of the
operations in Ti appears in the schedule before some conflicting operations in Tj.
Algorithm to test conflict serializability of a Schedule S:
1) For each transaction Ti participating in the schedule S, create a node lebelled Ti.
2) If Tj executes a read /(write) operation after Ti executes a write/(read) operation on any
data item, create an edge (Ti Tj) in the precedence graph.
3) If Tj executes a write operation after Ti executes a write operation on any data item Q,
create an edge (Ti Tj) in the precedence graph.
4) If the resultant precedence graph is a cyclic, then the schedule S is conflict serializable.
Otherwise, it is not conflict serializable.
T1 T2 T3
Read (P)
Read(R)
Read(P)
Read(R)
Read(Q)
Read(Q)
Write(P)
Write(R)
Write(Q)
Write(Q)
Concurrency Control:
Implementation:
When multiple transaction all trying to access the same shareable , there could arise many
problems and the access control is not done properly. There are some important mechanism by
which access control can be maintained.
Write Operation:
When a transaction writes some value into a data item the content of that data item remains in
an inconsistent state, starting from the moment when the writing operation begins up to the
moment the writing operation is over if we allow any other transaction to read or write the
value of data item during the write operation. Those transactions will read an inconsistent
value.
There are two types of locks:
1) Shared Lock:
A transaction may acquire shared lock in data item in order to read its content , the
lock is shared in the sense that any other transaction can acquire the shared lock on
that same data item for reading purpose.
T1 T2 T1 T2
A = A – 100 …
Unlock(A) …
Lock – X(B)
Read (B)
…….
Unlock (B)
2) Exclusive Lock:
A transaction may acquire exclusive lock on a data item in order to both read and write
into it. The lock is exclusive in the sense that no other transaction can acquire any kind
of lock on that same data item. Exclusive lock is used for update operation.
S X
S true False
X false False