DBMS Unit-6
DBMS Unit-6
GTU # 3130703
Unit-6
Transaction
Management
Outline
Looping
• What is transaction?
• ACID properties of transaction
• Transaction State Diagram \ State Transition Diagram
• Database recovery
• Concurrency
• Deadlock
Section – 1
What is transaction?
A transaction is a sequence of operations performed as a single logical unit of work.
A transaction is a logical unit of work that contains one or more SQL statements.
Example of transaction: Want to transfer Rs. 50 from Account-A to Account-B
Works as a single
logical unit
read (A)
A = A – 50
Transaction write (A) Operations
read (B)
B = B + 50
write (B)
Section – 2
ACID properties of transaction
Atomicity (Either transaction execute 0% or 100%)
Consistency (Database must remain in a consistent state after any transaction)
Isolation (Intermediate transaction results must be hidden from other concurrently executed
transactions)
Durability (Once a transaction completed successfully, the changes it has made into the
database should be permanent)
ACID properties of transaction (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. 0%
TheThis
Discover isthat
state the normal
initial
after state.
executionhas
the transaction canbeen
no longer
rolledproceed.
back and the
Thea transaction
Once
database has been stays
transaction cannotinbe
restoredthis state while
tocompleted,
its state it to
any
prior is executing.
changes that
the start of it
made
the must be undone rolling it back.
transaction.
Transaction State Diagram \ State Transition Diagram
Active
This is the initial state.
The transaction stays in this state while it is executing.
Partial Committed
When a transaction executes its final operation/ instruction, it is said to be in a partially committed state.
Failed
Discover that normal execution can no longer proceed.
Once a transaction cannot be completed, any changes that it made must be undone rolling it back.
Committed
The transaction enters in this state after successful completion of the transaction (after committing
transaction).
We cannot abort or rollback a committed transaction.
Aborted
The state after the transaction has been rolled back and the database has been restored to its state prior to
the start of the transaction.
Serializability
A schedule is serializable if it is equivalent to a serial schedule.
In serial schedules, only one transaction is allowed to execute at a time i.e. no concurrency is
allowed.
Whereas in serializable schedules, multiple transactions can execute simultaneously i.e.
concurrency is allowed.
Types (forms) of serializability
Conflict serializability
View serializability
View serializability
Let S1 and S2 be two schedules with the same set of transactions. S1 and S2 are view
equivalent if the following three conditions are satisfied, for each data item Q
Initial Read
Updated Read
Final Write
If a schedule is view equivalent to its serial schedule then the given schedule is said to be view
serializable.
Initial Read
If in schedule S1, transaction Ti reads the initial value of Q, then in schedule S2 also
transaction Ti must read the initial value of Q.
S1 S3 S2
T1 T2 T1 T2 T1 T2
Read (A) Read (A) Write (A)
Write (A) Write (A) Read (A)
Above two schedules S1 and S3 are not view equivalent because initial read operation in S1 is
done by T1 and in S3 it is done by T2.
Above two schedules S1 and S2 are view equivalent because initial read operation in S1 is
done by T1 and in S2 it is also done by T1.
Updated Read
If in schedule S1 transaction Ti executes read(Q), and that value was produced by transaction
Tj (if any), then in schedule S2 also transaction Ti must read the value of Q that was produced
by transaction Tj.
S1 S3 S2
T1 T2 T3 T1 T2 T3 T1 T2 T3
Write (A) Write (A) Write (A)
Write (A) Write (A) Read (A)
Read (A) Read (A) Write (A)
Above two schedules S1 and S3 are not view equal because, in S1, T3 is reading A that is
updated by T2 and in S3, T3 is reading A which is updated by T1.
Above two schedules S1 and S2 are view equal because, in S1, T3 is reading A that is updated
by T2 and in S2 also, T3 is reading A which is updated by T2.
Final Write
If Ti performs the final write on the data value in S1, then it also performs the final write on
the data value in S2.
S1 S3 S2
T1 T2 T3 T1 T2 T3 T1 T2 T3
Write (A) Write (A) Read (A)
Read (A) Write (A) Write (A)
Write (A) Read (A) Write (A)
Above two schedules S1 and S3 are not view equal because final write operation in S1 is done
by T3 and in S3 final write operation is also done by T1.
Above two schedules S1 and S2 are view equal because final write operation in S1 is done by
T3 and in S2 also the final write operation is also done by T3.
View serializable example
If a schedule is view equivalent to its serial schedule then the given schedule is said to be view
serializable.
Non-Serial Schedule (S1) Serial Schedule (S2)
T1 T2 T1 T2
Read (A) Read (A)
Write (A) Write (A)
Read (A) Read (B)
Write (A) Write (B)
Read (B) Read (A)
Write (B) Write (A)
Read (B) Read (B)
Write (B) Write (B)
S2 is the serial schedule of S1. If we can prove that they are view equivalent then we can says
that given schedule S1 is view serializable.
View serializable example (Initial Read)
Non-Serial Schedule (S1) Serial Schedule (S2)
T1 T2 T1 T2
Read (A) Read (A)
Write (A) Write (A)
Read (A) Read (B)
Write (A) Write (B)
Read (B) Read (A)
Write (B) Write (A)
Read (B) Read (B)
Write (B) Write (B)
In schedule S1, transaction T1 first reads the data item X. In S2 also transaction T1 first reads
the data item X.
In schedule S1, transaction T1 first reads the data item Y. In S2 also the first read operation on
Y is performed by T1.
The initial read condition is satisfied for both the schedules.
View serializable example (Updated Read)
Non-Serial Schedule (S1) Serial Schedule (S2)
T1 T2 T1 T2
Read (A) Read (A)
Write (A) Write (A)
Read (A) Read (B)
Write (A) Write (B)
Read (B) Read (A)
Write (B) Write (A)
Read (B) Read (B)
Write (B) Write (B)
In schedule 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 schedule 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 updated read condition is also satisfied for both the schedules.
View serializable example (Final Write)
Non-Serial Schedule (S1) Serial Schedule (S2)
T1 T2 T1 T2
Read (A) Read (A)
Write (A) Write (A)
Read (A) Read (B)
Write (A) Write (B)
Read (B) Read (A)
Write (B) Write (A)
Read (B) Read (B)
Write (B) Write (B)
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.
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.
The final write condition is also satisfied for both the schedules.
View serializable example
Non-Serial Schedule (S1) Serial Schedule (S2)
T1 T2 T1 T2
Read (A) Read (A)
Write (A) Write (A)
Read (A) Read (B)
Write (A) Write (B)
Read (B) Read (A)
Write (B) Write (A)
Read (B) Read (B)
Write (B) Write (B)
Since all the three conditions that checks whether the two schedules are view equivalent are
satisfied in this example, 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.
Section – 5
Database recovery
There are many situations in which a transaction may not reach a commit or abort point.
Operating system crash
DBMS crash
System might lose power (power failure)
Disk may fail or other hardware may fail (disk/hardware failure)
Human error
In any of above situations, data in the database may become inconsistent or lost.
For example, if a transaction has completed 30 out of 40 write instructions to the database
when the DBMS crashes, then the database may be in an inconsistent state as only part of the
transaction’s work was completed.
Database recovery is the process of restoring the database and the data to a consistent state.
This may include restoring lost data up to the point of the event (e.g. system crash).
Log based recovery method
The log is a sequence of log records, which maintains information about update activities on
the database.
A log is kept on stable storage (i.e HDD).
Log contains
Start of transaction
Transaction-id
Record-id
Type of operation (insert, update, delete)
Old value, new value
End of transaction that is committed or aborted.
Log based recovery method
When transaction Ti starts, it registers itself by writing a record <Ti start> to the log.
Before Ti executes write(X), a log record <Ti, X, V1, V2> is written, where V1 is the value of X
before the write (the old value), and V2 is the value to be written to X (the new value).
When Ti finishes it last statement, the log record <Ti commit> is written.
Undo of a log record <Ti, X, V1, V2> writes the old value V1 to X
Redo of a log record <Ti, X, V1, V2> writes the new value V2 to X
Types of log based recovery method
Immediate database modification
Deferred database modification
Section – 6
What is concurrency?
Concurrency is the ability of a database to allow multiple (more than one) users to access data
at the same time.
Three problems due to concurrency
Lost update problem
Dirty read problem
Incorrect retrieval problem
Lost update problem
This problem indicate that if two transactions T1 and T2 both X=100
read the same data and update it then effect of first update
T1 Time T2
will be overwritten by the second update.
--- T0 ---
How to avoid: A transaction T2 must not update the data
item (X) until the transaction T1 can commit data item (X). Read X T1 ---
--- T2 Read X
Update X
T3 ---
X=75
Update X
--- T4
X=50
--- T5 ---
Dirty read problem
The dirty read arises when one transaction update some item X=100
and then fails due to some reason. This updated item is
T1 Time T2
retrieved by another transaction before it is changed back to
the original value. --- T0 ---
How to avoid: A transaction T1 must not read the data item Update X
--- T1
(X) until the transaction T2 can commit data item (X). X=50
Read X T2 ---
--- T3 Rollback
--- T4 ---
Incorrect retrieval problem
The inconsistent retrieval problem arises Balance (A=200, B=250, C=150)
when one transaction retrieves data to use T1 Time T2
in some operation but before it can use Read (A)
this data another transaction updates that T1 ---
Sum 200
data and commits. Read (B)
T2 ---
Through this change will be hidden from Sum Sum + 250 = 450
first transaction and it will continue to use --- T3 Read (C)
previous retrieved data. This problem is Update (C)
--- T4
also known as inconsistent analysis 150 150 – 50 = 100
problem. --- T5 Read (A)
How to avoid: A transaction T2 must not Update (A)
--- T6
read or update data item (X) until the 200 200 + 50 = 250
transaction T1 can commit data item (X). --- T7 COMMIT
Read (C)
T8 ---
Sum Sum + 100 = 550
What is lock?
A lock is a variable associated with data item to control concurrent access to that data item.
Lock variable
01
Database
Lock based protocol
Data items can be locked in two modes :
Shared (S) mode: When we take this lock we can just read the item but cannot write.
Exclusive (X) mode: When we take this lock we can read as well as write the item.
T1
Lock-compatibility matrix
T2 Shared lock Exclusive lock
Yes No
Shared lock
Compatible Not Compatible
No No
Exclusive lock
Not Compatible Not Compatible
A transaction may be granted a lock on an item if the requested lock is compatible with locks
already held on the item by other transactions.
If a lock cannot be granted, the requesting transaction is made to wait till all incompatible
locks held by other transactions have been released. The lock is then granted.
Any number of transactions can hold shared locks on an item, but if any transaction holds an
exclusive on the item no other transaction can hold any lock on the item.
Lock based protocol
This locking protocol divides transaction execution phase into three parts:
1. When transaction starts executing, create a list of data items on which they need locks and requests the
system for all the locks it needs.
2. Where the transaction acquires all locks and no other lock is required. Transaction keeps executing its
operation.
3. As soon as the transaction releases its first lock, the third phase starts. In this phase a transaction cannot
demand for any lock but only releases the acquired locks.
Transaction
Lock acquisition execution Lock releasing
phase phase
Transaction
Transaction Transaction Time
begin end
Two phase locking protocol
This protocol works in two phases,
1. Growing Phase
In this phase a transaction obtains locks, but can not release any lock.
When a transaction takes the final lock is called lock point.
2. Shrinking Phase
In this phase a transaction can release locks, but can not obtain any lock.
The transaction enters the shrinking phase as soon as it releases the first lock after crossing the Lock
Point.
Transaction
Transaction Transaction Time
begin end
Strict two phase locking protocol V/S Rigorous two phase locking protocol
Strict two phase locking protocol
In this protocol, a transaction may release all the shared locks after the Lock Point has been reached, but it
cannot release any of the exclusive locks until the transaction commits or aborts.
It ensures that if data is being modified by one transaction, then other transaction cannot read it until first
transaction commits.
This protocol solves dirty read problem.
A deadlock is a situation in which two or more transactions are waiting for one another to give
up locks.
Deadlock detection
A simple way to detect deadlock is with the help of wait-for graph.
One node is created in the wait-for graph for each transaction that is currently executing.
Whenever a transaction Ti is waiting to lock an item X that is currently locked by a transaction
Tj, a directed edge from Ti to Tj (Ti→Tj) is created in the wait-for graph.
When Tj releases the lock(s) on the items that Ti was waiting for, the directed edge is dropped
from the wait-for graph.
We have a state of deadlock if and only if the wait-for graph has a cycle.
Then each transaction involved in the cycle is said to be deadlocked.
Deadlock detection
Transaction A is waiting for transactions B and C.
Transactions C is waiting for transaction B. B D
Wait-die Wound-wait
O needs a resource held by Y O waits Y dies
Y needs a resource held by O Y dies Y waits
Deadlock prevention
Following schemes use transaction timestamps for the sake of deadlock prevention alone.
3. Timeout-Based Schemes
A transaction waits for a lock only for a specified amount of time. After that, the wait times out and the
transaction is rolled back. So deadlocks never occur.
Simple to implement; but difficult to determine good value of the timeout interval.