DBMS_Module5.1 Transaction Processing
DBMS_Module5.1 Transaction Processing
1:
Transaction Processing,
Concurrency Control, and
Schedules
ACID
Inconsistency Problems
State Diagram
Types of Schedules
Precedence Graph Algorithm
2
Database Management Systems
Basic Concepts
3
Database Management Systems
Sequential Vs Concurrent Execution
B B
Time
4
Database Management Systems
ACID
Atomic: Either it succeeds as a whole, or all trace of it
disappears ('all or none').
Consistent: That is, halfway through the transaction in
the database may be in an inconsistent state, but after
the execution of the transaction, the database is in a
consistent state.
Isolation: Transactions are isolated, or protected, from
the effects of concurrently scheduling other
transactions.
Durability: It survives software and hardware crashes.
Concurrency Control
- ensures Consistency and Isolation
Logging and Recovery
- ensures Atomicity and Durability
5
Database Management Systems
Atomicity
Account A Account B
Transfer Rs. 2000
Name: John Name:
Bal: Rs. Smith
15000 1. Debit Bal: Rs.
A 500
2.
Credit B
Transactions may be incomplete for three reasons:
Aborted
System crash
Terminate itself, because of read error or disk
failure
6
Database Management Systems
Consistency
T1 T2
7
Database Management Systems
Isolation
The isolation property is ensured by
guaranteeing that, even transactions are
executed concurrently, the net effect is
identical to serial execution.
8
Database Management Systems
Durability
9
Database Management Systems
Notations
T – Transaction
Begin – Beginning of a transaction
R – Read action
W – Write action
R(A) – Read from database object A
W(A) – Write into database object A
End – End of a transaction
Commit – Store permanently
Abort – Abort a transaction
10
Database Management Systems
Schedule
A schedule is a set of transactions executed
either sequentially or concurrently
Schedule shows the order of execution
Example for a simple schedule:
T1 T2
R(A)
R(B)
A=A+5 Time
W(A)
B = B – 10
W(B) 11
Database Management Systems
Read & Write ACTIONS OF A TRANSACTION
13
Database Management Systems
1. Lost Update Problem (RW Conflict)
Assume: X = 80, N = 5, and M = 4.
Example:
T1 T2
R(X)
X=X–N
R(X)
X=X+M
W(X)
Commit
W(X)
Commit
14
Database Management Systems
2. The Dirty Read Problem (WR Conflict)
T1 T2
A = 500 R(A)
A = 400 A = A – 100
A = 400 W(A)
R(A) A = 400
A = A * 1.06 A = 424
W(A) A = 424
R(B) B = 700
B = B * 1.06
W(B)
B = 742
B = 742
T1 → T2
Commit
B = 600 R(B)
B = 700 B = B + 100
B = 700 W(B)
Commit
T1 T2
A = 500 R(A)
A = 400 A = A – 100
A = 400 W(A)
R(A) A = 400
A = A * 1.06 A = 424
W(A) A = 424
R(B) B = 600 Interleave (T1, T2)
B = B * 1.06 B = 636
W(B) B = 636
Commit
B = 636 R(B)
B = 736 B = B + 100
B = 736 W(B)
15
Commit
Database Management Systems
3. Blind-Write problem (WW Conflict)
16
Database Management Systems
3. Blind-Write problem (WW Conflict)
Example 2
17
Database Management Systems
3. Blind-Write problem (WW Conflict)
Example 3
18
Database Management Systems
Concurrent Transactions and Issues
5. Phantom Read Problem
• The phantom read problem happens when a transaction reads a variable once but then encounters
an error stating that the variable does not exist when it tries to read it again.
T1 T2
Read (A)
Read (A)
Delete (A)
Read (A)
Table 1
• In the example above Table 1, after transaction 2 reads the variable A, transaction 1 deletes the variable A
without the knowledge of transaction 2. As a result, when transaction 2 attempts to read A, it is unable to
do so.
19
Database Management Systems
State diagram of a Transaction
Read/Write
End Commit
Begin PARTIALLY
PARTIALLY
ACTIVE COMMITTED COMMITTED
COMMITTED
ACTIVE COMMITTED
Abort
Abort
FAILED TERMINATED
TERMINATED
FAILED
20
Database Management Systems
Transaction Model
The Recovery manager keeps track of the following operations…
Undo: It works in a similar way as rollback, however, it only affects a single
operation rather than the entire transaction.
Redo:
This specifies that specific transaction activities must be performed in order to
guarantee that a committed transaction's operations have been successfully
implemented in the database.
Force writing a log:
– Before a transaction reaches its commit point, any portion of the log that has
not been written to the disk yet must now be written to the disk.
– This process is called force-writing the log file before committing a
transaction.
Database Systems and Applications BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
21
Why Recovery is needed?
There are several possible reasons for a transaction to fail:
– A computer failure: A hardware, software, or network error occurs in the
computer system during transaction execution.
– A transaction or system error: Some operations in the transaction may
cause it to fail such as integer overflow or division by zero. Transaction
failure may also occur because of erroneous parameter values or because
of a logical programming error. In addition, the user may interrupt the
transaction during its execution.
– Local errors or exception conditions Detected by the transaction. For
example, an insufficient account balance in a banking database may cause
a transaction, such as a fund withdrawal from that account, to be
cancelled.
Database Systems and Applications BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
22
Why Recovery is needed?
– Concurrency control enforcement: The concurrency control method
may decide to abort the transaction. Because it violates
serializability or because several transactions are in a state of
deadlock
– Disk failure: all disks or some disk blocks may lose their data because
of a disk read/write head crash
Database Systems and Applications BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
23
Why Recovery is needed?
The System Log
The system maintains a log to keep track of all transaction operations that
affect the values of database items. This log may be needed to recover
from failures.
Types of log records :
– [start_transaction,T] : indicates that transaction T has started execution.
– [write_item,T,X,old_value,new_value] : indicates that transaction T has changed the
value of database item X from old_value to new_value. (new_value may not be
recorded)
– [read_item,T,X]: indicates that transaction T has read the value of database item X.
(read_item may not be recorded)
– [commit,T]: transaction T has been recorded permanently.
– [abort,T]: indicates that transaction T has been aborted.
Database Systems and Applications BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
24
Types of Schedule
Complete schedule
Recoverable and Unrecoverable
schedules
Serial and Non-serial schedules
Strict schedule
Serializable schedule
Conflict equivalent schedules
Conflict serializable schedules
25
Database Management Systems
Complete schedule
A schedule S is one which specifies
the ordering of n transactions
T1, T2, … , Tn.
T1 T2 T1 T2
R(A) R(A)
W(A) W(A)
R(B) R(B)
W(B) W(B)
Commit Commit
Commit Commit
Fig. 1 SA Fig. 2 SB
b1;
b1; r1(A);
r1(A); w1(A);
w1(A); b2;
b2; r2(B);
r2(B); w2(B);
w2(B); e1;
e1; c1;
c1; e2;
e2; c2.
c2.
26
Database Management Systems
Recoverable and Unrecoverable Schedules
29
Database Management Systems
Strict Schedule: Example
T1 T2 T1 T2
W(X)
R(X) W(X)
R(Y) Abort
Commit
W(Y)
W(X)
Commit
R(X) (b) Not a strict schedule
W(X) T2 has modified the value
of X before T1 could
Commit
commit or abort.
32
Database Management Systems
Example (Non-Conflict Equivalent)
S1 S2
T1 T2 T1 T2
R(X) W(X)
W(X) R(X)
T1 reads X as 10 T1 reads X as 20
S3 S4
T1 T2 T1 T2
W(X) W(X)
W(X) W(X)
Algorithm Precedence_Graph()
{
Step-1: Draw nodes corresponding to each of the
transaction in S.
Step-2: Draw an edge from Ti to Tj if Ti precedes and
conflicts with Tj. Complete the
precedence graph.
(The conflicting actions are RW, WR, and WW).
T1 T2
w1(X) < r2(X)
T1 T2
r1(X) < w2(X)
37
Database Management Systems
Example - 3
S = { b2, r2(Z), r2(Y), w2(Y), b3, r3(Y), r3(Z),
b1, r1(X), w1(X), w3(Y), w3(Z), e3, c3,
r2(X), r1(Y), w1(Y), e1, c1, w2(X), e2, c2 }
X
T1 T2
Y
Z
Y
Y
T3
39
Database Management Systems