Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMP9120
Transaction Management
4
Outline
› What is a transaction?
› Four properties of transaction: Atomicity, Consistency, Isolation, Durability
- What is the meaning of this property? Why we need this property? How is this property
ensured to hold?
5
Transfer $100
from Account A
to Account B
Motivation
Account A
Withdraw $100
Account B
Deposit $100
System recovers but database state no
longer reflects amount of physical money
available (short $100)
Account balance successfully updated
for Account A
Transfer Operation
*System fails*
Should group withdraw & deposit operations
together – so that they either both succeed, or
none happen at all
6
BEGIN;
Withdraw $100 from Account A;
Deposit $100 into Account B;
COMMIT;
What is a transaction?
› When an event occurs in the real world that changes the state of the enterprise,
a program is executed to change the database state in a corresponding way
- e.g., Bank balance must be updated on 2 accounts when a transfer is made
› Such an execution of a program is often modelled as a transaction:
a collection of one or more operations on one or more databases, which
reflects a discrete unit of work
- Transactions should conform to certain requirements (ACID properties):
e.g: In the real world, a transaction either completes or does not happen at all
7
Required properties of a transaction
› Atomicity: A transaction is either
performed entirely or not performed at all.
› Consistency: A correct execution of a
transaction must take a database from one
consistent state to another.
› Isolation: Effect of multiple transactions is
the same as these transactions running one
after another.
› Durability: Once a transaction changes the
database and the changes are committed,
these changes must never be lost because
of subsequent failure.
8
Outline
› Consistency
- What, why, and how?
› Atomicity
› Durability
› Isolation
9
Transactions should be Consistent
› A transaction is consistent if, assuming the database is in a consistent state
initially (satisfying all constraints), when the transaction completes:
1. All database constraints are satisfied
2. The new database state satisfies specifications of the transaction
› Each transaction should preserve the consistency of the database.
› Note that this is mainly the responsibility of the application developer!
- Database cannot 'fix' the correctness of a badly coded transaction
- An example bad transaction for bank transfer
- Withdraw $100 from account A, but only deposit $90 into account B.