- We need the ability to control how
transactions are run in a multiuser database.
- A transaction is a set of read
and write operations that must either
commit or abort.
- Consider the following transaction that
reserves a seat on an airplane flight and
changes the customer:
1. Read customer information
2. Write reservation information
3. Write charges
- Suppose that after the second step, the
database crashes. Or for some reason, changes
can not be written...
- Transactions can either reach a commit
point, where all actions are permanently saved
in the database or they can
abort in which case none of the actions
are saved.
- Another way to say this is transactions are
Atomic. All operations in a transaction
must be executed as a single unit - Logical
Unit of Work.
- Consider two users, each executing similar
transactions:
Example #1:
User A User B
Read Salary for emp 101 Read Salary for emp 101
Multiply salary by 1.03 Multiply salary by 1.04
Write Salary for emp 101 Write Salary for emp 101
Example #2:
User A User B
Read inventory for Prod 200 Read inventory for Prod 200
Decrement inventory by 5 Decrement inventory by 7
Write inventory for Prod 200 Write inventory for Prod 200
- First, what should the values for salary (in
the first example) really be ?
- The DBMS must find a way to execute these two
transactions concurrently and ensure
the result is what the users (and designers)
intended.
- These two are examples of the Lost
Update or Concurrent Update
problem. Some changes to the database can be
overwritten.
- Consider how the operations for user's A and B
might be interleaved as in example #2.
Assume there are 10 units in inventory for
Prod 200:
Read inventory for Prod 200 for user A
Read inventory for Prod 200 for user B
Decrement inventory by 5 for user A
Decrement inventory by 7 for user B
Write inventory for Prod 200 for user A
Write inventory for Prod 200 for user B
Or something similar like:
Read inventory for Prod 200 for user A
Decrement inventory by 5 for user A
Write inventory for Prod 200 for user A
Read inventory for Prod 200 for user B
Decrement inventory by 7 for user B
Write inventory for Prod 200 for user B
- In the first case, the incorrect amount (3)
is written to the database. This is called the
Lost Update problem because we lost the
update from User A - it was overwritten by user B.
- The second example works because we let user A write
the new value of Prod 200 before user B can read it.
Thus User B's decrement operation will fail.
- Here is another example. User's A and B share
a bank account. Assume an initial balance of $200.
User A reads the balance
User A deducts $100 from the balance
User B reads the balance
User A writes the new balance of $100
User B deducts $100 from the balance
User B writes the new balance of $100
- The reason we get the wrong final result
(remaining balance of $100) is because
transaction B was allowed to read stale
data. This is called the inconsistent
read problem.
- Suppose, instead of interleaving (mixing) the
operations of the two transactions, we execute
one after the other (note it makes no
difference which order: A then B, or B then
A)
User A reads the balance
User A deducts $100 from the balance
User A writes the new balance of $100
User B reads the balance (which is now $100)
User B deducts $100 from the balance
User B writes the new balance of $0