accessing data transactions. agenda questions from last class? transactions concurrency locking...
TRANSCRIPT
Accessing data
Transactions
Agenda
• Questions from last class?
• Transactions • concurrency • Locking• rollback
Transactions• 1+ SQL statements that are performed
completely, or not at all
• ACID properties – Atomic– Consistent– Isolation– Durability
• Why develop these?
Banking
Customer Accounthas
Transactions
T1
T2
T3
Checking
Savings
Loan
T1 – CustomerA checks her balance and withdraws money from checking account
T2 – An automatic withdrawal from CustomerA’s checking account goes towards a mortgage loan that she holds with the same bank
T3 – An automatic transfer moves money from CustomerA’s checking account into her savings account
Concurrency Problems: Lost Update
T1 - read R1T2 - read R1T1 - write new R1T2 - write new R1
Concurrency Problems: Temporary Update or Dirty Read
T1 - read RT1 - update RT2 - read RT1 - rollback to original value of R
Concurrency Problems: Incorrect Summary
T1 - read
T2 - update
R1
R2
R3
R1R2 R3
Concurrency Problems: Phantom Record
T1 - query DB, retrieve set of records
T2 - insert new record that satisfies T1’s query
Dealing with concurrency (OR: how to handle the pressure!)
• Timestamp ordering• Locking• Multiversion protocols• Optimistic protocols (validation
protocol)
Locks
• Granularity – lock on table, record, page, ets.
• Escalation – raising granularity in the middle of a transaction
• Index locking – primarily for adding/deleting records, but also changes to index field
Locking: Binary Lock
• 2 states: locked or unlocked• Rules:
1. Transaction must issue lock on item before doing any reads or writes of it.
2. Transaction must issue unlock of item after completing all reads and writes of it.
Locking: Multiple-mode Lock
• 2 types of locks:Shared, S, or Read lock. Any number of
S locks are allowed on an item.Exclusive, X, or Write lock. If an item is
X-locked, there can be no other locks on it.
• Transaction must have appropriate lock on item before performing the action.
Locking: Multiple-mode Lock (1)
• Rules:1. Transaction must issue S or X lock
before reading an item.2. Transaction must issue an X lock
before writing an item.3. Transaction must release locks after
all reads and writes are completed.
Locking: Multiple-mode Lock (2)
4. Transaction may upgrade S lock to X lock if no one else has S lock.
5. Transaction may downgrade X lock to S lock when all writes are completed.
Intention locking
• Acts on next higher level – to write to a record, acquire intention lock on table and then acquire lock on record
• 3 types of locks– Intention read– Intention write– Read intention write
• Must check for locks on at least two levels
Concurrency with Locking:Lost Update
T1 - request S lock on RT1 - read R - S lockT2 - request S lock on RT2 - read R - S lockT1 - request X lock - waitT2 - request X lock - wait
Concurrency with Locking:Temporary Update
T1 - request S lock on RT1 - read R - S lockT1 - request X lock on RT1 - update R - X lockT2 - request S lock on R - waitT1 - release X lock on RT2 - read R - S lock
Concurrency with Locking:Incorrect Summary (1)
T1 - request S lock on R1T1 - read R1 - S lockT1 - request S lock on R2T1 - read R2 - S lockT2 - request S lock on R3T2 - read R3 - S lock
Concurrency with Locking:Incorrect Summary (2)
T2 - request X lock on R3T2 - update R3 - X lockT2 - request S lock on R1T2 - read R1 - S lockT2 - request X lock on R1 - waitT1 - request S lock on R3 - wait
Concurrency with Locking:Deadlock
• Simplest example:T1 - write R1 - X lockT2 - write R2 - X lockT1 - request X lock on R2 - waitT2 - request X lock on R1 - wait