Unit No.5(Transaction Processing)
Unit No.5(Transaction Processing)
5
Transaction Processing
Transaction Concept:-
A transaction is a single logical unit of work which
accesses and possibly modifies the contents of a
database. Transactions access data using read
and write operations.
In order to maintain consistency in a database,
before and after the transaction, certain
properties are followed. These are
called ACID properties.
A transaction in a database system must
maintain Atomicity, Consistency, Isolation,
and Durability − commonly known as ACID
properties − in order to ensure accuracy,
completeness, and data integrity.
ACID Properties:-
Atomicity − This property states that a
transaction must be treated as an atomic unit,
that is, either all of its operations are executed or
none. There must be no state in a database where
a transaction is left partially completed. States
should be defined either before the execution of
the transaction or after the
execution/abortion/failure of the transaction. It
involves the following two operations.
—Abort: If a transaction aborts, changes made to
database are not visible.
—Commit: If a transaction commits, changes
made are visible.
Atomicity is also known as the ‘All or nothing rule’.
• Consider the following
transaction T consisting of T1 and T2:
Transfer of 100 from account X to account Y.
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
Durability − The database should be durable
enough to hold all its latest updates even if the
system fails or restarts. If a transaction updates a
chunk of data in a database and commits, then the
database will hold the modified data. If a
transaction commits but the system fails before the
data could be written on to the disk, then that data
will be updated once the system springs back into
action.
Transaction States in DBMS:-
States through which a transaction goes during its lifetime. These are
the states which tell about the current state of the Transaction and
also tell how we will further do processing we will do on the
transactions. These states govern the rules which decide the fate of
the transaction whether it will commit or abort.
There are different types of Transaction States :-
1] Active State –
When the instructions of the transaction is running then
the transaction is in active state. If all the read and write
operations are performed without any error then it goes
to “partially committed state”, if any instruction fails it
goes to “failed state”.
2] Partially Committed –
After completion of all the read and write operation the
changes are made in main memory or local buffer. If the
the changes are made permanent on the Data Base then
state will change to “committed state” and in case of
failure it will go to “failed state”.
3] Failed State-
When any instruction of the transaction fails it goes to
“failed state” or if failure occurs in making permanent
change of data on Data Base.
4] Aborted State –
After having any type of failure the transaction goes
from “failed state” to “aborted state” and in before
states the changes are only made to local buffer or
main memory and hence these changes are deleted
or rollback.
5] Committed State –
It is the stage when the changes are made
permanent on the Data Base and transaction is
complete and therefore terminated in “terminated
state”.
6] Terminated State –
If there is any roll back or the transaction come from
“committed state” then the system is consistent and
ready for new transaction and the old transaction is
terminated.
Concurrent Execution of Transaction:-
In the transaction process, a system usually allows
executing more than one transaction simultaneously.
This process is called a concurrent execution.
Advantages of concurrent execution of a
transaction:-
1.Decrease waiting time or turnaround time.
2.Improve response time
3.Increased throughput or resource utilization.
Concurrency problems:-
the same data item value and then update it, then
the second record overwrites the first record.
Example: Let’s take the value of A is 100
Transaction Transaction
Time
T1 T2
t1 Read(A)
t2 A=A-50
t3 Read(A)
t4 A=A+50
t5 Write(A)
t6 Write(A)
-Here,
• At t1 time, T1 transaction reads the value of A i.e., 100.
• At t2 time, T1 transaction deducts the value of A by 50.
• At t3 time, T2 transactions read the value of A i.e., 100.
• At t4 time, T2 transaction adds the value of A by 150.
• At t5 time, T1 transaction writes the value of A data item on the basis
of value seen at time t2 i.e., 50.
• At t6 time, T2 transaction writes the value of A based on value seen at
time t4 i.e., 150.
• So at time T6, the update of Transaction T1 is lost because Transaction
T2 overwrites the value of A without looking at its current value.
• Such type of problem is known as the Lost Update Problem.
,
2.Dirty read problem (W-R conflict)-
This type of problem occurs when one transaction T1 updates a data
item of the database, and then that transaction fails due to some
reason, but its updates are accessed by some other transaction.
Example: Let’s take the value of A is 100
Transaction Transaction
Time
T1 T2
t1 Read(A)
t2 A=A+20
t3 Write(A)
t4 Read(A)
t5 A=A+30
t6 Write(A)
t7 Write(B)
Here,
At t1 time, T1 transaction reads the value of A i.e., 100.
At t2 time, T1 transaction adds the value of A by 20.
At t3 time, T1transaction writes the value of A (120) in
the database.
At t4 time, T2 transactions read the value of A data
item i.e., 120.
At t5 time, T2 transaction adds the value of A data item
by 30.
At t6 time, T2transaction writes the value of A (150) in
the database.
At t7 time, a T1 transaction fails due to power failure
then it is rollback according to atomicity property of
transaction (either all or none).
So, transaction T2 at t4 time contains a value which has
not been committed in the database. The value read by
the transaction T2 is known as a dirty read.
3.Unrepeatable read (R-W Conflict)-
It is also known as an inconsistent retrieval