Transaction Processing
Transaction Processing
Transaction
Processing
Outline
Looping
• What is transaction?
• ACID properties of transaction
• Transaction State Diagram \ State Transition Diagram
• Schedule
• Two phase commit protocol
• Database recovery
• Concurrency
• Deadlock
What is transaction?
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)
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 ischanges
executing.
that
the start of it
made
the must be undone rolling it back.
transaction.
Ti Tj Ti Tj
1. li = read(Q), lj = read(Q)
read (Q) read (Q)
li and lj don’t conflict read (Q) read (Q)
Ti Tj Ti Tj
2. li = read(Q), lj = write(Q) read (Q) write(Q)
li and lj conflict write(Q) read (Q)
Ti Tj Ti Tj
write(Q) read (Q)
3. li = write(Q), lj = read(Q)
read (Q) write(Q)
li and lj conflict
Ti Tj Ti Tj
write(Q) write(Q)
4. li = write(Q), lj = write(Q) write(Q) write(Q)
li and lj conflict Unit 7 – Transaction Processing 25
Conflict serializability
If a given schedule can be converted into a serial schedule by swapping its non-conflicting
operations, then it is called as a conflict serializable schedule.
T1 T2 T1 T2
Read (A) Read (A)
A = A - 50 A = A - 50
Write (A) Write (A)
Read (A) Read (B)
Temp = A * 0.1 B = B + 50
A = A - temp Write (B)
Write (A) Commit
Read (B) Read (A)
B = B + 50 Temp = A * 0.1
Write (B) A = A - temp
Commit Read (B) Write (A)
B = B + temp Read (B)
Write (B) B = B + temp
Commit Write (B)
Commit
Unit 7 – Transaction Processing 27
Conflict serializability (Example)
Example of a schedule that is not conflict serializable:
T1 T2
Read (A)
Write (A)
Read (A)
We are unable to swap instructions in the above schedule to obtain either the serial schedule
<T1, T2>, or the serial schedule <T2, T1>.
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.
Re q u e s
t to pre
pare
Prepare
Phase Prepared
Co m m
it/Abor
t
Commit
Phase Done
Coordinator
Coordinator
Participant
Send inform
send
send
“ack” to to do
request
reply
inform
commit
asking for
whether ready
ready
commit toto
commit
done or
commit
or
notnot
Time TC Tf
T1
T2
T3
T4
At failure time:
Ignore the transaction T1 as it has already been committed before checkpoint.
Redo transaction T2 and T3 as they are active after checkpoint and are committed before failure.
Undo transaction T4 as it is active after checkpoint and has not committed.
Time TC TC Tf
T1
T2
T3
T4
T5
T6
Exercise Give the name of transactions which has already been committed before checkpoint.
Exercise Give the name of transactions which will perform Redo operation.
Exercise Give the name of transactions which will perform Undo operation.
Two pages - page 2 & 5 - are affected by a transaction and copied to new physical pages. The
current page table points to these pages.
The shadow page table continues to point to old pages which are not changed by the
transaction. So, this table and pages are used for undoing the transaction.
Lock variable
01
Database
Transaction
Lock acquisition execution Lock releasing
phase phase
Transaction
Transaction Transaction Time
begin end
Transaction
Transaction Transaction Time
begin end
T1 T2
(A)
Lock-X
Granted for (A)
Write (A)
Lock-X (B) Granted for (B)
Write (B)
Lock-X (A) Waiting for (A)
Write (A)
A deadlock is a situation in which two or more transactions are waiting for one another to give
up locks.
K
OC
DL
Suppose now that transaction D is requesting an item held by C.
A
DE
Then the edge D → C is added to the wait-for graph. C
Now this graph contains the cycle.
B→D→C→B
It means that transactions B, D and C are all deadlocked.
K
OC
Choosing which transaction to abort is known as victim selection.
DLA
In this wait-for graph transactions B, D and C are deadlocked.
DE
C
In order to remove deadlock one of the transaction out of these
three (B, D, C) transactions must be roll backed.
We should rollback those transactions that will incur the minimum
cost.
When a deadlock is detected, the choice of which transaction to
abort can be made using following criteria:
The transaction which have the fewest locks
The transaction that has done the least work
The transaction that is farthest from completion
Unit 7 – Transaction Processing 73
Deadlock prevention
A protocols ensure that the system will never enter into a deadlock state.
Some prevention strategies :
Require that each transaction locks all its data items before it begins execution (pre-declaration).
Impose partial ordering of all data items and require that a transaction can lock data items only in the order
specified by the partial.
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