Professional Documents
Culture Documents
Topic 6a - Transactions Management
Topic 6a - Transactions Management
Topic 6a - Transactions Management
ConcurrencyControl
Database Recovery
1 Concurrency Control
Transactions Management
Transactions Management deals with the problem of
always keeping the database in a consistent state even
when concurrent accesses and failures occur
The database can (and usually is) temporarily
inconsistent during the execution of a transaction. The
important point is that the database should be
consistent when the transaction terminates
2 Concurrency Control
Transaction
A transaction is a series of actions, carried out by a
single user or application program, which must be
treated as a logical unit of work.
A transaction is a unit of consistent and reliable
computation.
Example the transfer of funds from one account to
another: either the entire operations is carried out or
none of the operation is carried out.
Transactions transform the database from consistent
state to another consistent state.
3 Concurrency Control
Transactions in SQL
Consider the following SQL statement
UPDATE Project
SET Budget=Budget*1.1
WHERE Pname=‘Engineering’
This can be translated into a transaction as follows:
Begin_Transaction Budget_Update
Begin
EXEC SQL UPDATE Project
SET Budget=Budget*1.1
WHERE Pname=‘Engineering’
End
4 Concurrency Control
Transaction in SQL
5 Concurrency Control
Properties of a Transaction: ACID
Atomicity
– Transactions are either done or not done
– They are never left partially executed
Consistency
– Transactions should leave the database in a consistent state
Isolation
– Transactions must behave as if they were executed in isolation
Durability
– Effects of completed transactions are resilient against failures
6 Concurrency Control
Concurrency Control
Goal: ensure the “I” (Isolation) in ACID
T1 : T2 :
read(A); read(A);
write(A); write(A);
read(B); read(C);
write(B); write(C);
commit; commit;
A B C
7 Concurrency Control
DBMS Components
Transactions Manager - It is the responsibility of the
Transactions Manager of a DBMS to ensure transactions do not
interfere with one another and corrupt the database.
Scheduler - Responsible for implementing a particular strategy for
concurrency control. Also called the Lock Manager (for those
systems that use locking as the means of managing concurrency)
Recovery Manager- Ensures that the database is restored to the
state it was in before the start of the transaction.
Buffer Manager - Responsible for the transfer of data between
disk storage and memory.
8 Concurrency Control
Conflicting Operations
Two operations on the same data item conflict if
at least one of the operations is a write
– r(X) and w(X) conflict
– w(X) and r(X) conflict
– w(X) and w(X) conflict
– r(X) and r(X) do not
– r/w(X) and r/w(Y) do not
Order of conflicting operations matters
– If T1 .r(A) precedes T2 .w(A), then conceptually, T1 should
precede T2
9 Concurrency Control
Interference Between
Concurrent Transactions
10 Concurrency Control
The Lost Update Problem
11 Concurrency Control
Example: Bank Balances
Transaction A: retrieves some record R at time t1
Transaction B: retries the same record R at time t2
Transaction A: updates record at time t3 on the
basis of values read at time t1
Transaction B updates the same record at time t4
on the basis of values read at
12 Concurrency Control
Solutions to the Lost Update
Problem
The loss of Transaction A’s update is avoided by preventing
Transaction B from reading the value of R until after Transaction
A's update has been completed.
Write Lock R at t1
13 Concurrency Control
Uncommitted Dependency Problem
A violation of the integrity constraints governing the database can
arise when two transactions are allowed to execute concurrently
without being synchronized.
Consider this situation:
14 Concurrency Control
Example: Scheduling of Surgeons
for Operations
Transaction A reads an uncommitted update at time t2 e.g.
assigning Theatre to a Surgeon and a Patient
The update is then undone at time t3 e.g. Surgeon or the Patient
becomes Unavailable
Transaction A is therefore operating on false assumption
i.e.Transaction A becomes dependent on an uncommitted update
at time t2
15 Concurrency Control
Solution to the Uncommitted
Dependency Problem
The problem is avoided by preventing Transaction A from reading
the value of R until after the decision has been made either to
commit or abort Transaction B's effect.
Write Lock R at t1
16 Concurrency Control
Inconsistent Analysis Problem
17 Concurrency Control
Inconsistent Analysis Problem 2
18 Concurrency Control
Solution: Inconsistent Analysis Problem
19 Concurrency Control
Locking
20 Concurrency Control
Locking
21 Concurrency Control
Locking
22 Concurrency Control
Two Phase Locking
23 Concurrency Control
Problem of 2PL
T1 T2 T2 has read uncommitted data
r(A) written by T1
w(A)
r(A)
If T1 aborts, then T2 must abort as
w(A) well
r(B) Cascading aborts possible if other
w(B) transactions have read data written
r(B) by T2
r(B)
Abort!
What’s worse, what if T2 commits
before T1?
24 Concurrency Control
Strict 2PL
25 Concurrency Control
Deadlocks
Time Transaction A Transaction B
t1 Read Lock ACC1
t2 Read ACC1
t3 sum = sum + ACC1
t4 Read Lock ACC2
t5 Read ACC2
t6 sum = sum + ACC2 Write Lock ACC3
t7 wait Write ACC3
t8 wait Write Lock ACC1
t9 wait wait
t10 wait Wait
t11 wait Wait
Deadlock had occurred!
26 Concurrency Control
Dealing with Deadlocks
27 Concurrency Control
Concurrency Control without Locking
Optimistic (validation-based)
Timestamp-based
28 Concurrency Control
Optimistic (validation-based)
Locking is pessimistic
– Use blocking to avoid conflicts
– Overhead of locking even if contention is low
Optimistic concurrency control
– Assume that most transactions do not conflict
– Let them execute as much as possible
– If it turns out that they conflict, abort and restart
– Timestamp-based
29 Concurrency Control
Optimistic: Sketch of Protocol
30 Concurrency Control
Pessimistic vs Optimistic
31 Concurrency Control
Timestamp-based
32 Concurrency Control