Professional Documents
Culture Documents
56-Additional Material-12-07-2023
56-Additional Material-12-07-2023
56-Additional Material-12-07-2023
1. Begin Transaction
2. Read Balance (A) A transaction is an executing program
3. Compute Balance(A)-100 that forms a logical unit of database
4. Write Balance(A) processing.
5. Read Balance (B) A transaction includes one or more
6. Compute Balance(B)+100 database access operations—these can
7. Write Balance(B) include insertion, deletion, modification,
8. Commit or retrieval operations.
Read Balance(A)
Variable XA AA 400
500
400
Read Value into XA
BB 100
200
XA = XA-100
Write XA Main Memory
Variable BA XA=500
XA=400
Read Value of B into BA
BA=BA+100 BA=200
BA=100
Write BA
Commit;
A 500
400
B 100
200
Hard Disk
Transaction Processing Systems
• Transaction processing systems are systems
with large databases and hundreds of
concurrent users executing database
transactions.
• Examples of such systems include airline
reservations, banking, credit card processing,
online retail purchasing, stock markets,
supermarket checkouts, and many other
applications.
Concurrent Transactions
Need to Transfer
100 Rupees from
Account A to
Account B
Account A Account B
Balance - 500 Need to Transfer Balance - 100
500 Rupees from
Account C to
Account D
Account C Account D
Balance - 1000 Balance - 2000
T1 T2
1. Begin Transaction 1. Begin Transaction
2. Read Balance (A) 2. Read Balance (C)
3. Compute Balance(A)-100 3. Compute Balance(C)-500
4. Update Balance(A) 4. Update Balance(C)
5. Read Balance (B) 5. Read Balance (D)
6. Compute Balance(B)+100 6. Compute Balance(D)+500
7. Update Balance(B) 7. Update Balance(D)
8. Commit 8. Commit
Transaction Schedule
Time Transaction T1 Transaction T2
ts1 Begin Transaction
ts2 Read Balance (A)
ts3 Compute Balance(A)-100
ts4 Write Balance(A)
ts5 Read Balance (B)
ts6 Compute Balance(B)+100
ts7 Write Balance(B)
ts8 Commit
ts9 Begin Transaction
ts10 Read Balance (C)
ts11 Compute Balance(C)-500
ts12
ts13 Serial Execution Write Balance(C)
Read Balance (D)
Ts14 Compute Balance(D)+500
Ts15 of Transactions T1 Write Balance(D)
Ts16 Commit
and T2
Concurrent Execution of Transactions
Timestamp Transaction T1 Transaction T2
ts1 Begin Transaction
ts2 Read Balance (A) Begin Transaction
ts3 Compute Balance(A)-100
ts4 Read Balance (C)
ts5 Write Balance (A)
ts6 Compute Balance(C)-500
ts7 Read Balance (B)
ts8 Write Balance (C)
ts9 Compute Balance(B)+100
ts10 Read Balance (D)
ts11 Write Balance(B)
ts12 Compute Balance(D)+500
ts13 Commit
ts14 Write Balance(D)
ts15 Commit
Advantages of Concurrent Execution
• Waiting time of Transactions is Reduced
• Response time increases
• Resources are used effectively
Problems with Concurrent Execution
Timestamp Transaction T1 Transaction T2
ts1 Begin Transaction
ts2 Read Balance (A) Begin Transaction
ts3 Compute Balance(A)-100
ts4 Read Balance (A)
A
ts5 Write Balance (A) Balance :500
ts6 Compute Balance(A)-50
ts7 Read Balance (B)
ts8 Write Balance (A) B
Balance :100
ts9 Compute Balance(B)+100
ts10 Read Balance (C)
ts11 Write Balance(B)
C
ts12 Compute Balance(C)+50 Balance : 1000
ts13 Commit
ts14 Write Balance(C)
ts15 Commit
Lost Update Problem
• This problem occurs when two transactions
that access the same database items have
their operations interleaved in a way that
makes the value of some database items
incorrect.
• T2 reads the value of A before T1 changes it in
the database, and hence the updated value
resulting from T1 is lost
The Temporary Update (or Dirty Read) Problem.
Timestamp Transaction T1 Transaction T2
ts1 Begin Transaction
ts2 Read Balance (A) Begin Transaction
ts3 Compute Balance(A)-100
ts4 Write Balance (A) A
ts5 Read Balance (A) Balance :500
ts6 Compute Balance(A)-50
ts7 Read Balance (B)
B
ts8 Fails (ABORT)
Balance :100
ts9 Write Balance (A)
ts10 Read Balance (C)
ts12 Compute Balance(C)+50 C
Balance : 1000
ts14 Write Balance(C)
ts15 Commit
The Temporary Update (or Dirty Read)
Problem.
• This problem occurs when one transaction
updates a database item and then the
transaction fails for some reason.
• Meanwhile, the updated item is accessed
(read) by another transaction before it is
changed back to its original value.
The Incorrect Summary Problem
Timestamp Transaction T1 Transaction T2
ts1 Begin Transaction
ts2 Read Balance (A) Begin Transaction
ts3 Read Balance (A)
ts4 Compute Total = Total +
Balance (A) A
ts5 Compute Balance(A)-50 Balance :500
ts6 Read Balance (B)
ts7 Compute Total = Total +
Balance (B) B
ts8 Write Balance (A) Balance :100
T1
1. Begin Transaction
2. Read Balance (A)
3. Compute Balance(A)-100
4. Update Balance(A)
5. Read Balance (B)
6. Compute Balance(B)+100
7. Update Balance(B)
8. Commit
Concurrency Control Techniques
• Locking
Transaction uses locks to deny access to
other transactions and so prevent incorrect
updates.
Two Types of Locks
• Read_Lock(item)
• Write_Lock(item)
Read_Lock(item)
• Read-locked item is also called share-locked
because other transactions are allowed to
read the item.
T1 REA
D_ LOC
K(B
alan
ce)
Account A
T2 READ_LOCK(Balance)
Balance : 500
B a la nce)
D _LOCK(
REA
T3
Write_lock(item)
• If a transaction is to write an item X, it must have exclusive
access to X.
• Write-locked item is called exclusive-locked because a single
transaction exclusively holds the lock on the item.
T3 T2 REA
D _L
Wri
te_L OCK(B
OCK a
(Bal lance)
anc
e)
Account A
T1 Write_LOCK(Balance)
Balance : 500
Unlock(item)
• After performing the operations on a item the
transaction must unlock the item so that other
transactions can use the item.
Rules
1. A transaction T must issue the operation read_lock(X) or write_lock(X)
before any read_item(X) operation is performed in T.
2. A transaction T must issue the operation write_lock(X) before any
write_item(X) operation is performed in T.
3. A transaction T must issue the operation unlock(X) after all
read_item(X) and write_item(X) operations are completed in T
4. A transaction T will not issue a read_lock(X) operation if it already
holds a read (shared) lock or a write (exclusive) lock on item X.
5. Transaction T will not issue a write_lock(X) operation if it already holds
a read (shared) lock or write (exclusive) lock on item X.
6. A transaction T will not issue an unlock(X) operation unless it already
holds a read (shared) lock or a write (exclusive) lock on item X.
Lost Update Problem
Timestamp Transaction T1 Transaction T2
ts1 Begin Transaction
ts2 Read Balance (A) Begin Transaction
ts3 Compute Balance(A)-100
ts4 Read Balance (A)
ts5 Write Balance (A)
ts6 Compute Balance(A)-50
A
ts7 Read Balance (B)
Balance :500
ts8 Write Balance (A)
ts9 Compute Balance(B)+100
ts10 Read Balance (C) B
ts11 Write Balance(B) Balance :100
ts12 Compute Balance(C)+50
ts13 Commit
C
ts14 Write Balance(C)
Balance : 1000
ts15 Commit
Preventing Loss Update Problem using Locks
Timestamp Transaction T1 Transaction T2
ts1 Begin Transaction
ts2 Write_Lock(A)
ts3 Read Balance (A) Begin Transaction
ts4 Write_Lock(A) – Not
allowed Because Of Lock
ts5 Compute Balance(A)-100 Wait
ts6 Write Balance (A) Wait
A
ts7 Unlock (A) Wait
Balance :500
ts8 Write_Lock(A)
ts9 READ Balance (A)
ts10 Write_LOCK(B) B
ts11 READ Balance (B) Balance :100
ts12 Compute Balance(B)+100
ts13 Compute Balance(A)-50
C
ts14 Write Balance(A)
Balance : 1000
ts15 Unlock (A)
Timestamp Transaction T1 Transaction T2
ts1 Write Balance (B)
ts2 Unlock(B)
ts3 Write_Lock(C)
ts4 Read Balance(C)
ts5 Compute Balance(C)+50
ts6 Write Balance(C)
ts7 Unlock (C) A
ts8 Commit Balance :500
ts9 Commit
ts10
B
ts11 Balance :100
ts12
ts13
ts14 C
ts15 Balance : 1000
CAN SUCH LOCKS PREVENT
TEMPORARY UPDATE PROBLEM?
The Temporary Update (or Dirty Read) Problem.
Timestamp Transaction T1 Transaction T2
ts1 Begin Transaction
ts2 Read Balance (A) Begin Transaction
ts3 Compute Balance(A)-100
ts4 Write Balance (A)
ts5 Read Balance (A)
ts6 Compute Balance(A)-50 A
ts9 Write Balance (A) Balance :500
ts10 Read Balance (C)
ts11 Compute Balance(C)+50
B
ts12 Write Balance(C) Balance :100