L9: Transactions Flashcards
Transactions in SQL:
Basic Structure
- Transactions are typically “ad-hoc”
- Each statement is treated as one transaction
- But in a program, multiple statements can be grouped as a single transaction
- Must begin with START TRANSACTION
- Can have multiple UPDATE calls
- End with COMMIT
START TRANSACTION
UPDATE table_name
SET field = new_value
WHERE
COMMIT
Two Schemes/Approaches
for preventing
Deadlock
Wait-Die Scheme
Wound-Wait Scheme
*These schemes prevent deadlock and “starvation”
Preventing Deadlock:
Wait-Die Scheme
Wait-Die Scheme
- Non-preemptive
- Older transaction waits for the younger transaction to release lock
- Younger transactions never wait, they are rolled back instead
Preventing Deadlock:
Wound-Wait Scheme
Wound-Wait Scheme
- Preemptive
- Older Transaction “wounds” younger transaction
- Forces rollback instead of waiting
- Younger transactions wait for older transactions
Log Based Recover:
Basic Idea
- Keep a “log” of every operation on stable storage
- __Maintains a record of update activities on database
- When a transaction( T ) starts, it registers itself in the log
- <t></t>
- Before executing a write, noted in log
- <t></t>
- Whe T is done, commits the change
- <t></t>
Log Based Recovery:
Concepts/Points covered
- Basic Idea
- Checkpointing
- Modification Variants:
- Deferred Modification
- Immediate Modification
- Recovery Operations
- Undo
- Redo
- How disk is shared
- Locking Assumption
Log Based Recovery:
Checkpointing
- During recovery, only need to consider the most recent transaction that started before the checkpoint
- And consider all transactions after the most recent transaction (what?)
- Scan backwards from end of log to find checkpoint
- Only transactions that are in L, or started after the checkpoint need to be recovered
- Continue scanning back to find the start of all transactions in L
Log Based Recovery:
Recovery Operations
Undo( T )
Writes old value to X: <t></t>
Log updated with <t> and</t>
ended with <t> after transaction is finished</t>
Redo( T )
Writes new value to X: <t></t>
No log output for Redo
Log Based Recovery:
When to use
Undo
Undo needs to be performed IF:
- T contains the record <t></t>
- But does not contain <t> or <t></t></t>
Basically, the transaction was started but not finalized or aborted
Sometimes, if multiple failures occur,
then the actions of undoing operations may need to be performed AGAIN.
Log Based Recovery:
When to use
Redo
Redo needs to be performed IF:
- T contains the record <t></t>
- AND either <t> or <t></t></t>
basically, the Transaction was Started and Finished,
but is wrong.
Log Based Recovery:
Types of Modification
- Immediate Modification
- Updates written to disk before transaction commits
- Deferred Modification
- Updates written to disk when transaction is committed
Log Based Recovery:
Types of Modification:
Deferred Modification
Deferred Modification
- Only performs updates to buffer and disk at the time of the transaction commit
- Simplifies some of the recovery aspects
- Needs to store more local copies in the transactions
Log Based Recovery:
Types of Modification:
Immediate Modification
Immediate Modification
- Allows updates of uncommitted transaction to be made to buffer or disk before transaction commits
- Update log must be written before Database item written
- Assume log record is output directly to stable storage
- In reality, output to stable storage can take place at any time
- Order in which blocks are output can be different from the order in which they are written to the buffer
Log Based Recovery:
How disk and log access is allotted
All transactions share disk and the log
Log Based Recovery:
Multiple Transactions attempting modification
Assume that if
some transaction has modified an item,
no other transaction can modify the item
until
the original transaction has committed or aborted.
Log Based Recovery:
Log Update Visibility
Updates of other transactions should not be visible
Transactions:
Topics/Concepts
- Transaction syntax
- Granularity
- Disk Access
- Lock Compatibility Table
- Deadlock and deadlock prevention
- Log Based Recovery
- Recover Algorithm
Granularity:
Overview
- Can increase Concurrency by providing levels of granularity to the access
- Coarse or Fine granularity
- Accomplished with Hierarchical, lockable units
- Examples:
- Database, Relations/Files, Pages, Tuples, Attributes, etc
- Examples:
- Need to create a collision path:
- Locks different levels of access
- Intention Locks
- Locks different levels of access