Loading in 2 Seconds...
Loading in 2 Seconds...
Transaction Management, Concurrency Control and Recovery. Chapter 22. Overview. What are transactions? What is a schedule? What is concurrency control? Why we need concurrency control: Three problems. Serializabiltiy and Concurrency control: Theory: Conflict Serializability
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
Overview • What are transactions? • What is a schedule? • What is concurrency control? • Why we need concurrency control: • Three problems. • Serializabiltiy and Concurrency control: • Theory: • Conflict Serializability • View Serializability • Practice: • Locking • Time-stamping • Optimistic techniques • Recovery facilities
What is a Transaction? Transaction Action, or series of actions, carried out by user or application, which accesses or changes contents of database. • Logical unit of work on the database. • Transforms database from one consistent state to another, although consistency may be violated during transaction. • Example: Read(staffNo, salary) salary=salary * 1.1 write(staffNo , salary)
What is a Transaction? • Can have one of two outcomes: • Success - transaction commits and database reaches a new consistent state. • Failure - transaction aborts, and database must be restored to consistent state before it started (rolled back or undone). • Committed transaction cannot be aborted. • Aborted transactions that are rolled back can be restarted later.
Properties of Transactions Four basic (ACID) properties of a transaction are: Atomicity ‘All or nothing’ property. Consistency Must transform database from one consistent state to another. Isolation Partial effects of incomplete transactions should not be visible to other transactions. Durability Effects of a committed transaction are permanent and must not be lost because of later failure. • We deal with transactions in a schedule.
Schedule Data Items affected by transactions (optional) Start with t0 or t1 Running Transactions Order of execution
Schedule Rules • Never start two transactions at the same time. • Never perform Reads and Writes of different transactions at the same time. • Each transaction should end with a commit or abort (rollback).
Schedule Definitions Schedule Sequence of reads/writes by set of concurrent transactions. Serial Schedule Schedule where operations of each transaction are executed consecutively without any interleaved operations from other transactions. • No guarantee that results of all serial executions of a given set of transactions will be identical. (Think of an example) Non-Serial Schedule Schedule where operations from set of concurrent transactions are interleaved
What is Concurrency Control? Concurrency: transactions running simultaneously. Concurrency Control: Process of managing simultaneous operations (transactions) on the database without having them interfere with one another. • Prevents interference when two or more users are accessing database simultaneously and at least one is updating data. • Although two transactions may be correct in themselves, interleaving of operations may produce an incorrect result.
Why we Need Concurrency Control? Three examples of potential problems caused by concurrency: • Lost update problem. • Uncommitted dependency problem. • Inconsistent analysis problem.
Lost Update Problem • Successfully completed update is overridden by another user. • T1 withdrawing £10 from an account with balx, initially £100. • T2 depositing £100 into same account. • Serially, final balance would be £190. • Loss of T2’s update avoided by preventing T1 from reading balx until after update Time T1 T2 balx t1 begin-transaction 100 t2 begin-transaction read(balx) 100 t3 read(balx) balx = balx +100 100 t4 balx = balx -10 write(balx) 200 t5 write(balx) commit 90 t6 commit 90
Uncommitted Dependency Problem • Occurs when one transaction can see intermediate results of another transaction before it has committed. • T4 updates balx to £200 but it aborts, so balx should be back at original value of £100. • T3 has read new value of balx (£200) and uses value as basis of £10 reduction, giving a new balance of £190, instead of £90. • Problem avoided by preventing T3 from reading balx until after T4 commits or aborts. Time T3 T4 balx t1 begin-transaction 100 t2 read(balx) 100 t3 balx = balx +100 100 t4 begin_transaction write(balx) 200 t5 read(balx) : 200 t6 balx = balx -10 rollback 100 t7 write(balx) 190 t8 commit 190
Inconsistent Analysis Problem • Occurs when transaction reads several values but second transaction updates some of them during execution of first. • Sometimes referred to as dirty read or unrepeatable read. • T6 is totaling balances of account x (£100), account y (£50), and account z (£25). • Meantime, T5 has transferred £10 from balx to balz, so T6 now has wrong result (£10 too high).
Inconsistent Analysis Problem • Problem avoided by preventing T6 from reading balx and balz until after T5 completed updates.
Serializability • Serializability is a property of a schedule: • We say serializable schedule and non-serializable schedule. • But what makes a schedule serializable? • A serializable schedule is a non-serial schedule that allows transactions to execute concurrently without interfering with one another. • In other words, a non-serial schedule that is equivalent to someserial schedule. • Main goal is to prevent transactions interfering with each other (3 problems discussed earlier).
Serializability • Two types of seriailizability: • Conflict. • View. Serializability Theory Practice Conflict Serializability Test View Serializability Test Locking Time-stamping Optimistic
Conflict Serializability • In serializability, ordering of read/writes is important: (a) If two transactions only read a data item, they do not conflict and order is not important. (b) If two transactions either read or write completely separate data items, they do not conflict and order is not important. (c) If one transaction writes a data item and another reads or writes same data item, order of execution is important. They conflict.
Conflict Serializability • Schedule S1 is conflict serializable if it is conflict equivalent to a serial schedule. • We test a schedule for conflict serialiazibility using a Precedence Graph.
Testing for Conflict Serializability – Precedence Graph • Create: • node for each transaction; • a directed edge Ti Tj, if Tj reads the value of an item written by Ti; • a directed edge Ti Tj, if Tj writes a value into an item after it has been read by Ti. • a directed edge Ti Tj, if Tj writes a value into an item after it has been written by Ti. • If precedence graph contains cycle, schedule is not conflict serializable.
Test Schedule: Is it conflict serializable? Time T7 T8 t1 begin-transaction t2 read(balx) t3 balx = balx +100 t 4 write(balx) t5 begin_transaction t6 read(balx) t7 balx = balx * 1.1 t8 write(balx) t9 read(baly) t10 baly = baly * 1.1 t11 write(baly) t12 commit t13 read(baly) t14 write(baly) t15 commit
View Serializability • Offers less stringent definition of schedule equivalence than conflict serializability. • Two schedules S1 and S2 are view equivalent if: • For each data item x, if Ti reads initial value of x in S1, Ti must also read initial value of x in S2. • For each read on x by Ti in S1, if value read by x is written by Tj, Ti must also read value of x produced by Tj in S2. • For each data item x, if last write on x performed by Ti in S1, same transaction must perform final write on x in S2.
View Serializability • Schedule is view serializable if it is view equivalent to a serial schedule. • Every conflict serializable schedule is view serializable, although converse is not true. • It can be shown that any view serializable schedule that is not conflict serializable contains one or more blind writes. All Schedules View Serializable Schedules Conflict Serializable Schedules
View Serializable Schedule Time T7 T8 t1 begin-transaction t2 read(balx) t3 write(balx) t4 read(baly) t5 write(baly) t6 commit t7 begin-transaction t8 read(balx) t9 write(balx) t10 read(baly) t11 write(baly) t12 commit T7 T8 begin-transaction read(balx) write(balx) begin_transaction read(balx) write(balx) read(baly) write(baly) commit read(baly) write(baly) commit
View Serializable Schedule Time T11 T12 T13 t1 begin-transaction t2 read(balx) t3 begin_transaction t4 write(balx) t5 commit t6 write(balx) t7 commit t8 begin_transaction t9 write(balx) t10 commit Is this schedule conflict serializable?
Concurrency Control Techniques Serializability Theory Practice Conflict Serializability Test View Serializability Test Locking Time-stamping Optimistic
Concurrency Control Techniques • Two basic concurrency control techniques: • Locking, • Timestamping. • Both are conservative approaches: delay transactions in case they conflict with other transactions. • Optimistic methods assume conflict is rare and only check for conflicts at commit.
Concurrency Control Techniques Overview Locking Time-stamping Optimistic Basic Rules 2PL Deadlock Prevention Deadlock Detection Basic Time-stamp Ordering Multi-version Time-stamp Ordering Regular Rigorous Thomas’s Write Rule Time outs Wait-Die Wound-Wait Wait-for Graph Strict
Locking Main Idea: Transaction uses locks to deny access to other transactions and so prevent incorrect updates. • Most widely used approach to ensure serializability. • A transaction must claim: • a shared (read) on x before it can read it. • or an exclusive (write) lock on x before it can write it. • Lock prevents other transactions from reading or writing the locked data item.
Locking – Basic Rules • Shared Lock: • If transaction has shared lock on item, it can read but not update item. • More than one transaction can hold a shared lock on an item. • Exclusive Lock: • If transaction has exclusive lock on item, can both read and update item. • Only one transaction can hold an exclusive lock on an item. • Some systems allow transaction to: • upgrade read lock to an exclusive lock. • downgrade exclusive lock to a shared lock.
Locking -- Commands • To acquire a shared (read) lock on X: • Read_Lock(x) • RLock(X) • Shared_Lock(X) • SLock(X) • To acquire an exclusive (write) lock on X: • Write_Lock(X) • WLock(X) • Exclusive_Lock(X) • XLock(X) • To release a lock on X: • Unlock(X)
Time T9 T10 t1 begin-transaction t2 write_lock(balx) t3 read(balx) t4balx = balx + 100 t5 write(balx) t6 unlock(balx) t7 begin_transaction t8 write_lock(balx) t9 read(balx) t10 balx = balx * 1.1 t11 write(balx) t12 unlock(balx) t13 write_lock(baly) t14 read(baly) t15 baly = baly * 1.1 t16 write(baly) t17 commit/unlock(baly) t18 write_lock(baly) t19 read(baly) t20baly = baly - 100 t21 write(baly) t22 commit/unlock(baly) Correct use of locks. But is the execution correct?
Two-Phase Locking (2PL) • We just saw that locking alone doesn’t always work. • Solution: 2PL. Transaction follows 2PL protocol if all locking operations precede first unlock operation in the transaction. • Two phases for transaction: • Growing phase - acquires all locks but cannot release any locks. • Shrinking phase - releases locks but cannot acquire any new locks. • With 2PL, we can prevent the three problems.
Original Lost Update Problem Time T1 T2 balx t1 begin-transaction 100 t2 begin-transaction read(balx) 100 t3 read(balx) balx = balx +100 100 t4 balx = balx -10 write(balx) 200 t5 write(balx) commit 90 t6 commit 90
Preventing Lost Update Problem Time T1 T2 balx t1 begin-transaction 100 t2 begin_transaction write_lock(balx) 100 t3 write_lock(balx) read(balx) 100 t4 WAIT balx = balx +100 100 t5 WAIT write(balx) 200 t6 WAIT commit/unlock(balx) 200 t7 read(balx) 200 t8 balx = balx -10 200 t9 write(balx) 190 t10 commit/unlock(balx) 190
Original Uncommitted Dependency Problem Time T3 T4 balx t1 begin-transaction 100 t2 read(balx) 100 t3 balx = balx +100 100 t4 begin_transaction write(balx) 200 t5 read(balx) : 200 t6 balx = balx -10 rollback 100 t7 write(balx) 190 t8 commit 190
Preventing Uncommitted Dependency Problem Time T3 T4 balx t1 begin-transaction 100 t2 write_lock(balx) 100 t3 read(balx) 100 t4 begin_transaction balx = balx +100 100 t5 write_lock(balx) write(balx) 200 t6 WAIT commit/unlock(balx) 200 t7 read(balx) 200 t8 balx = balx -10 200 t9 write(balx) 190 t10 commit/unlock(balx) 190
Cascading Rollbacks • If every transaction in a schedule follows 2PL, schedule is serializable. • However, problems can occur with interpretation of when locks can be released. • Cascading rollback is undesirable since they potentially lead to the undoing of a significant amount of work • To prevent this with 2PL, 2 solutions: • Rigorous 2PL: Leave release of all locks until end of transaction. • Strict 2PL: Holds only exclusive locks until the end of the transaction. • BOTH are still 2PL. So both still have growing and shrinking phases. • 2PL still may cause deadlock.
Problems with 2PL • Cascading Rollbacks: • Solved with strict or rigorous 2PL. • Dead Locks: • Happen in regular 2PL, and also in strict and rigorous 2PL. • Handled using deadlock detection and prevention techniques.
Deadlocks Deadlock: An impasse that may result when two (or more) transactions are each waiting for locks held by the other to be released. • Once a deadlock happens, only one way to break deadlock: abort one or more of the transactions. • Deadlock should be transparent to user, so DBMS should restart aborted transaction(s).
Example Deadlock Time T9 t1 begin-transaction t2 write_lock(balx) t3 read(balx) t4balx = balx - 10 t5 write(balx) t6 write_lock(baly) t7 WAIT t8 WAIT t9 WAIT t10 WAIT t11 : T10 begin-transaction write_lock(baly) read(baly) baly = baly + 100 write(baly) wait_lock(balx) WAIT WAIT WAIT :
Deadlock Handling • Two general techniques for handling deadlock: • Deadlock prevention: DBMS doesn’t allow deadlock to happen. • Timeouts. • Wait-Die. • Wound-wait. • Deadlock detection and recovery: DBMS allows deadlocks to happens but detects and recovers from them. • Wait-for Graphs (WFG).
Timeouts • Transaction that requests lock will only wait for a system-defined period of time. • If lock has not been granted within this period, lock request times out. • DBMS assumes transaction deadlocked, even though it may not be, and it aborts and automatically restarts the transaction.
Timestamps Before we discuss Wait-die and Wound-wait techniques, introduce timestamps. A timestamp is a unique number given to each transaction. Traditionally, it is the time the transaction started. The smaller the timestamp, the older the transaction.
Timestamps Time T11 T12 T13 t1 begin-transaction t2 read(balx) t3 begin_transaction t4 write(balx) t5 commit t6 write(balx) t7 commit t8 begin_transaction t9 write(balx) t10 commit • TS(T11) = 1 • TS(T12) = 3 • TS(T13) = 8
Wait-Die Technique • Only an older transaction can wait for younger one, otherwise transaction is aborted (dies) and restarted with same timestamp. (Why the same?) • If a transaction Ti requests a lock on an item held by Tj: • If Ti > Tj [TS(Ti) < TS(Tj)], Ti waits for Tj to release the lock. • If Ti < Tj [TS(Ti) > TS(Tj)], Ti is aborted and restarted with the same TS.