Lect-Transactions-1-Week 10 (TEL)
Lect-Transactions-1-Week 10 (TEL)
DATABASE
SYSTEM PRINCIPLES
TRANSACTION MANAGEMENT
BACKGROUND
So far
What happens?
• When two queries attempt to update the same data
item?
• System fails during query execution?
RETRIEVE-ONLY QUERIES
Is it a problem? No!
Why?
Lost Updates
ONE-PASS ALGORITHMS
What is a transaction?
TRANSACTION
What is it?
Example
Move $1,000 from Freddy’s account to his bookie’s account.
Transaction
• Check whether Freddy has $1,000.
• Deduct $1,000 from his account.
• Add $1,000 to his bookie’s account.
STRAWMAN APPROACH
Handling failures
• Before a transaction starts, copy the entire database to a
new file and make all changes to that file.
• If the transaction completes successfully, overwrite the
original file with the new one.
• If the transaction fails, just remove the dirty copy.
CAN DO BETTER!
Better approach
• Utilisation/ throughput
• Increased response times to users.
• Correctness
• Fairness
• Hard problems!
DEFINITION OF A TRANSACTION
Conceptually
Consists of?
• A sequence of read and write operations on the database,
together with computation steps.
TRANSACTIONS IN SQL
Start of a transaction
End of transaction
• A transaction stops with either COMMIT or ABORT.
• If commit, all changes are saved.
• If abort, all changes are undone so that it’s like as if the
transaction never executed at all.
• Abort can be either self-inflicted or caused by the DBMS.
EXAMPLE
ONE-PASS ALGORITHMS
Atomicity
A transaction is either performed in its entirety (can be in
steps) or not performed at all.
Consistency
If T starts with database in consistent state +
T executes in isolation ⇒ T leaves consistent state
Isolation
A transaction should appear as though it is executed in
isolation from other transactions.
Durability
Changes applied to the database by a committed transaction
must persist in the database.
ATOMICITY OF TRANSACTIONS
DBMS guarantee
Shadow paging
Consistency
Two types
• Database consistency
• Transaction consistency
DATABASE CONSISTENCY
State of a database
• State changes occur due to updates
• Modifications
• Insertions
• Deletions
Consistent State
Execution of Transaction T
Begin End
Transaction T Transaction T
TRANSACTION CONSISTENCY
What is it?
• Actions of concurrent transactions.
• Database should be in consistent state even if there are a
number of user requests that are concurrently accessing
(reading or updating) the database.
Database in a Database in a
consistent consistent
state state
ISOLATION OF TRANSACTION
Isolation
• Users submit transactions, and each transaction
executes as if it was running by itself.
• Appear as though it is executed in isolation from
other transactions.
Implications
An executing transaction cannot reveal its results to other
concurrent transactions before its commitment.
Concurrency
Protocol
Durable changes
How it is enforced?
ONE-PASS ALGORITHMS
Assumption 1
Assumption 2
State of a transaction
Database state
Operation Semantics
START TRANSACTION
READ(X, t) Copy database
element X to
READ(A,t);
transaction’s local t := t*2;
variable t WRITE(A,t);
WRITE(X, t) copy transaction READ(B,t);
local variable t to t := t*2;
element X
WRITE(B,t);
INPUT(X) read element X to
COMMIT;
memory buffer
ONE-PASS ALGORITHMS
Transaction management
Deal with the problems of always keeping the database in a consistent
state even when concurrent accesses and failures occur.