Professional Documents
Culture Documents
Principles of Transaction Management: Pang Hwee Hwa School of Computing, NUS Panghh@comp - Nus.edu - SG
Principles of Transaction Management: Pang Hwee Hwa School of Computing, NUS Panghh@comp - Nus.edu - SG
Principles of Transaction Management: Pang Hwee Hwa School of Computing, NUS Panghh@comp - Nus.edu - SG
H. Pang / NUS
Outline
• Performance tuning
H. Pang / NUS
Application
Programmer
(e.g., business analyst,
Data architect)
Application
Sophisticated
Application Query Processor
Programmer
(e.g., SAP admin)
Indexes Storage Subsystem
Hardware
H. Pang / NUS [Processor(s), Disk(s), Memory]
Transaction Concepts & Protocols
• Transaction
– A logical unit of database processing
– A sequence of begin, reads/writes, end
– Unit of recovery, consistency, concurrency
H. Pang / NUS
Transaction States
read-item,
write-item
begin- end-
transaction transaction Partially commit
Active Committed
Committed
abort
abort
Transition
Failed Terminated
STATE
H. Pang / NUS
Interleaved Transactions
A A
B B
Time
t1 t2 t3 t4 t5
H. Pang / NUS
Transaction “Correctness”
• ACID properties
– Atomicity
– Consistency
– Isolation
– Durability
H. Pang / NUS
Serial Schedule
• Schedule
– A sequence of read & write operations from various transactions
– R1[X] W3[Y] R2[X] W2[Y] W1[X] W2[X]
• Serial schedule
– No interleaved operations from the participating transactions
– W3[Z] R3[Y] R1[X] W1[Y] R2[Y] W2[Z] W2[X]
– Always correct, but … so slow!
H. Pang / NUS
Equivalent Schedules
• Examples
1. R1[X] W2[X] R3[Y] W1[Y] R2[Y] W3[Z] W2[Z]
2. W3[Z] R3[Y] R1[X] W1[Y] R2[Y] W2[Z] W2[X]
3. W2[X] R1[X] W1[Y] R2[Y] W3[Z] W2[Z] R3[Y]
• 1 and 2 are equivalent; not 3
H. Pang / NUS
Serializable Schedule
Theorem
• A schedule is serializable if there is a serial
schedule such that for every conflicting pair of
operations, the two operations appear in the
same order in both schedules.
H. Pang / NUS
Concurrency Control Enforces Serializability
H. Pang / NUS
Concurrency Control Protocols
• Others mechanisms
– Multi-version and optimistic protocols
• Granularity issues
H. Pang / NUS
Locking
H. Pang / NUS
Types of Locks
• Binary locks
– Locked, or Unlocked
• Check before enter; wait when locked; lock after enter;
unlock after use (and wakeup one waiting transaction).
– Simple but too restrictive
H. Pang / NUS
Read/Write Locking Scheme
H. Pang / NUS
Does Locking Ensure Serializability?
T1 T2
read-lock (Y); read-lock (X);
Y unlocked read-item (Y); read-item (X); X unlocked
too early too early
unlock (Y); unlock (X);
write-lock (X); write-lock (Y);
read-item (X); read-item (Y);
X:=X+Y; Y:=X+Y;
write-item (X); write-item (Y);
unlock (X); unlock (Y);
Cannot serialize T1 and T2
H. Pang / NUS
Need for Locking Protocol
H. Pang / NUS
Two-Phase Locking (2PL)
H. Pang / NUS
2PL Variants
• Basic 2PL
• Conservative 2PL
– Locking operations precede transaction execution
– Make sure can acquire necessary locks
• Strict 2PL
– Unlocking of write-locks after commit (or abort)
– Avoid cascading abort
• Rigorous 2PL
– Unlocking of all locks after commit (or abort)
H. Pang / NUS
Limitations of 2PL
H. Pang / NUS
Lock Granularity
H. Pang / NUS
Other CC Protocols
• Timestamp based
• Multi-version based
• Optimistic concurrency control
– No checking is done before or during transaction
execution
– The transaction is validated at the end of execution,
by checking if serializability has been violated
H. Pang / NUS
Summary of Transaction Concepts
Baseline:
Transaction
Serial Schedule
Correctness
Other CC Protocols
A Strict 2PL
• Timestamp
C • Multi-version
I 2PL • Optimistic
D
Ideal:
Serializable Schedule
H. Pang / NUS
Summary
To improve performance
Interleave transactions
Correctness: ACID
• Lock contention
• Deadlock
H. Pang / NUS
Performance Impact of Concurrency Control
H. Pang / NUS