Lect-Transactions-Week 11
Lect-Transactions-Week 11
SYSTEM PRINCIPLES
TRANSACTION MANAGEMENT
a c i d
Atomicity: Consistency: Isolation: Durability:
Transactions Only valid Transactions Written
are all or data is do not data will not
nothing saved affect each be lost
other
ISOLATION
Motivation
T1 Seq of Exec.
x= 50
READ(x) T1: READ(x)
x := x+1 T1: x := x+1
WRITE(x) T1: WRITE(x)
Commit T1: Commit
T2: READ(x)
T2: x := x+1
T2: WRITE(x)
T2
T2: Commit
READ(x)
x := x+1
WRITE(x)
Commit
CONCURRENT TRANSACTIONS
T1 Seq of Exec.
x= 50 Ensure isolation by not
READ(x) T1: READ(x) permitting incomplete
x := x+1 T1: x := x+1 results to be seen by
WRITE(x) T2: READ(x) other transactions
Commit T1: WRITE(x)
T2: x := x+1
T2: WRITE(x)
T1: Commit
T2
T2: Commit
READ(x)
x := x+1
WRITE(x) • T2 reads 50
Commit • Value of x is 51 at the
end of T1 and T2 since
T2’s WRITE overwrites
T1’s WRITE
CONCURRENT TRANSACTIONS
ONE-PASS ALGORITHMS
• Concurrency Control
CONCURRENCY CONTROL
Concurrency Control
Who is responsible?
• READ(A)
• ABORT(A)
Dirty Read
T1 T2
A := 20; B := 20;
WRITE(A)
READ(A)
READ(B)
WRITE(B)
Inconsistent Read
UNREPEATABLE READ & LOST UPDATE
T1 T2
READ(A)
WRITE(A)
READ(A)
Unrepeatable Read
T1 T2
READ(A)
READ(A)
A:= A + 5
A:= A* 1.3
WRITE(A)
WRITE(A);
Lost Update
UNREPEATABLE READ & LOST UPDATE
Lost Update Model
Write
Acquire Debit
User 1 record to
Customer Account
Customer
Record for $400
table.
Bal = $500 Bal = $100
Bal = $100
Time
Write
Acquire Credit
record to
User 2 Customer Account
Customer
Record for $200
table.
Bal = $500 Bal = $700
Bal = $700
T1
READ(x)
x := x+1
WRITE(x) If the concurrent
Commit execution of transactions
leave the database in a
state that can be
achieved by their serial
T2 execution in some order,
READ(x) then problem solved!
x := x+1
WRITE(x)
Commit
T1 T2
DB consistent state:
READ(A, t) READ(A, s) A=B
t := t+100 s := s*2
WRITE(A, t) WRITE(A, s)
READ(B, t) READ(B, s)
t := t+100 s := s*2
WRITE(B, t) WRITE(B, s)
SERIAL SCHEDULE
T1 T2 A B
25 25
What is it?
READ(A, t)
t := t+100
A schedule is serial if its actions
consists of all the actions of one WRITE(A, t) 125
transaction, then all the actions READ(B, t)
of another transaction, and so on
t := t+100
WRITE(B, t) 125
Final state is not independent of READ(A, s)
the order of transactions s := s*2
WRITE(A, s) 250
READ(B, s)
s := s*2
WRITE(B, s) 250
SERIALIZABLE SCHEDULE
T1 T2 A B
Motivation 25 25
READ(A, t)
• Every serial schedule will t := t+100
preserve DB consistency
WRITE(A, t) 125
• Are there any other schedules
that also are guaranteed to READ(A, s)
preserve consistency? s := s*2
WRITE(A, s) 250
Serializable Schedule READ(B, t) Not serial but
t := t+100 the effect is
A schedule is serializable if it is WRITE(B, t) the same 125
equivalent to a serial schedule
READ(B, s)
s := s*2
WRITE(B, s) 250
A NON-SERIALIZABLE SCHEDULE
T1 T2 A B
25 25
This sort of behaviour
READ(A, t)
that concurrency
control mechanism
t := t+100
must avoid!
WRITE(A, t) 125
READ(A, s)
s := s*2
WRITE(A, s) 250
READ(B, s)
s := s*2
WRITE(B, s) 50
READ(B, t)
t := t+100
WRITE(B, t) 150
IGNORE TRANSACTION SEMANTICS
Avoid unrealism
Assumption
T1
READ(A, t)
t := t+100
WRITE(A, t)
READ(B, t)
T1: r1(A); w1(A); r1(B); w1(B)
t := t+100
WRITE(B, t)
T2
READ(A, s)
s := s*2
WRITE(A, s)
T2: r2(A); w2(A); r2(B); w2(B) READ(B, s)
s := s*2
WRITE(B, s)
CONFLICT-SERIALIZABILITY
Commercial systems
Conflict
Implication 1
Implication 2
Two actions by
ri(X); rj(Y) same transactions
ri(X); wi(Y)
Read/write by Ti, Tj
wi(X); wj(Y) for X != Y to same element
wi(X); rj(X)
ri(X); wj(X)
CONFLICT-SERIALIZABILITY
Conflict-Serializable
How to Test?
Is a schedule conflict-serializable ?
Precedence Condition
Precedence Graph
These are exactly the
Summarize the precedence conditions under which
using precendence graph we cannot swap A1 and A2
THE PRECEDENCE GRAPH TEST
Precedence Graph
1 2 3
Conflict serializable
T1 <S T2
1. A1 is ahead of A2 in S
2. Both A1 and A2 involve the same database element;
and
3. At least one of A1 and A2 is a write-action
PRECENDENCE GRAPH
1 2 3
Not conflict serializable
T1 <S T2
1. A1 is ahead of A2 in S
2. Both A1 and A2 involve the same database
element; and
3. At least one of A1 and A2 is a write-action
CONFLICT-SERIALIZABILITY
Generate a conflict-serializable
schedule for the execution of pending
transactions
Algorithms
Pessimistic Optimistic
Timestamp Timestamp
Locking Hybrid Locking
Ordering Ordering
.
CHARACTERISTICS OF LOCKS
Consistency of Transactions
• A transaction can only read or write an element if it
previously was granted a lock on that element and
hasn’t released the lock yet.
• If a transaction locks an element, it must later
unlock that element.
Legality of Schedules
• No two transactions may have locked the same
element without one having first released the lock.
Notation
li(A) = transaction Ti acquires lock for element A
ui(A) = transaction Ti releases lock for element A
EXAMPLE
T1 T2 A B
Legal schedule as T1 and
25 25 T2 never hold a lock on
l1(A), r1(A); A or B at the same time.
A := A+100
But not serializable!
w1(A), u1(A); 125
l2(A); r2(A);
A := A*2
w2(A); u2(A); 250
l2(B); r2(B);
B := B*2
w2(B); u2(B); 50
l1(B), r1(B);
B := B+100
w1(B), u1(B); 150
ISSUE
B := B*2
w2(B); u2(B); 250
TWO-PHASE LOCKING (2PL)
2PL Rule
In every transaction, all lock actions precede all unlock
actions.
Two phases
• First phase – Locks are obtained.
• Second phase – Locks are relinquished.
• A transaction that obeys 2PL condition is called a
two-phase-locked transaction (2PL transaction).
EXAMPLE
T1 T2 A B Does not obey 2PL condition.
25 25
l1(A), r1(A);
A := A+100
w1(A), u1(A); 125
l2(A); r2(A);
A := A*2
w2(A); u2(A); 250
l2(B); r2(B);
B := B*2
w2(B); u2(B); 50
l1(B), r1(B);
B := B+100
w1(B), u1(B); 150
2PL TRANSACTIONS
T1 T2 A B
l1(A), r1(A);
A := A+100
w1(A); l1(B); u1(A); 125
l2(A); r2(A);
A := A*2
w2(A); 250
l2(B) DENIED
r1(B);
B := B+100
w1(B), u1(B); 125
l2(B); u2(A); r2(B);
B := B*2
w2(B); u2(B); 250
A RISK OF DEADLOCK
.
A METAPHOR – ART EXHIBITION
Background
• Too simple to be a practical scheme!
• T must take a lock on a database element X even if it
only wants to read X and not write it.
• No reasons why several transactions can’t read X
at the same time!
REVISIT METAPHOR
LOCK MODES
Types of Locks
• Shared lock (For READ)
• Exclusive lock (For WRITE)
• Update lock
• Initially like shared lock.
• Later may be upgraded to exclusive lock.
SHARED AND EXLUSIVE LOCKS
Notation
sli(A) = transaction Ti requests a shared lock for element A
xli(A) = transaction Ti requests an exclusive lock for element A
CHARACTERISTICS OF LOCKS
Consistency of Transactions
• In any transaction Ti, ri(X) must be preceded by sli(X) or
xli(X), with no intervening ui(X).
• In any transaction Ti, wi(X) must be preceded by xli(X),
with no intervening ui(X).
2PL of Transactions
• In any 2PL transaction Ti, no action sli(X) or xli(X) can be
preceded by an action ui(Y), for any Y.
CHARACTERISTICS OF LOCKS
Legality of Schedules
An element may be locked either exclusively by one
transaction or by several in shared mode, but not both.
Legality of Schedules
• If xli(X) appears in a schedule, then there cannot be a
following xlj(X) or slj(X) for some j≠i without an
intervening ui(X).
• If sli(X) appears in a schedule, then there cannot be a
following xlj(X) for j≠i, without an intervening ui(X).
EXAMPLE
T1 T2 Legality of schedule
sl1(A); r1(A); If sli(X) appears in a schedule, then
sl2(A); r2(A); there cannot be a following xlj(X)
for j≠i, without an intervening ui(X).
sl2(B); r2(B);
xl1(B) DENIED
u2(A); u2(B);
xl1(B); r1(B); w1(B);
u1(A); u1(B);
COMPATIBILITY MATRIX
What is it?
• Describe lock management policies involving several
lock modes.
Lock requested
S X
T1 T2 T1 T2
sl1(A); r1(A); sl1(A); r1(A);
sl2(A); r2(A); sl2(A); r2(A);
sl2(B); r2(B); sl2(B); r2(B);
xl1(B) DENIED sl1(B); r1(B);
u2(A); u2(B); xl1(B) DENIED
xl1(B); r1(B); w1(B); u2(A); u2(B);
u1(A); u1(B); xl1(B); w1(B);
u1(A); u1(B);
UPDATE LOCKS
T1 T2
sl1(A)
sl2(A)
xl1(A) DENIED
xl2(A) DENIED
Update Lock
• An update lock uli(X) gives Ti only the privilege to read X,
not to write X.
• Only the update lock can be upgraded to a write lock
later.
• A read lock cannot be updated.
• An update lock can be granted on X even if sli(X) exists.
• Once uli(X) is given, no other modes can be taken on X.
UPDATE LOCKS
Reading
UPDATE LOCKS
Lock requested
S X U
S Yes No Yes
Lock held in
X No No No
mode
U No No No
T1 T2 T1 T2
sl1(A) ul1(A); r1(A);
sl2(A) ul2(A) DENIED
xl1(A) DENIED xl1(A);
w1(A); u1(A);
xl2(A) DENIED
ul2(A); r2(A);
xl2(A); w2(A); u2(A);
WORKING OF SCHEDULER
Question Assumptions
How does a scheduler use these • Transactions themselves do not request locks.
locking schemes? • Job of the scheduler to insert lock actions.
• Transactions do not release locks.
• Scheduler releases them when the transaction
manager tells it that the transaction is
READ(A); committed/ aborted.
Inserts appropriate lock
actions ahead of all DB WRITE(B);
access operations. COMMIT(T);…..
• If T aborts/ commits, it Scheduler (Part 1)
is notified by Trans
Manager.
• Releases all locks held
LOCK(A); READ(A); …..
by T. Lock
• Notifies Part 2 for
Table
waiting transactions. Scheduler (Part 2)
Determines if
Determines theT isnext
transaction
delayed as aorlock has not READ(A); WRITE(B);
transactions
been grantedthatandcan
now be given
maintains a lock list.
a waiting on X.
CONCLUSIONS
Concurrency Control
Interleaves actions (reads/writes) of various transactions
while preserving consistency