Chap16 17 Transaction Con Currency

59
Database Management Systems Chapters 16 & 17: Transaction and Concurrency Control  Acknowledgement: This slide is based upon the slides by the authors and others

Transcript of Chap16 17 Transaction Con Currency

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 1/59

Database Management Systems

Chapters 16 & 17:

Transaction and Concurrency Control

Acknowledgement:This slide is based upon the slides

by the authors and others

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 2/59

Goals (= Objectives) of CS566E SS EN TIAL OBJ E CTIVE S: (1)TO GAI N FACTUAL K N OWLED G E

(terminology, classifications, methods, etc.)and (2) TO L E ARN FU ND AMEN TALPRI N CIPL E S, G ENE RALIZATIO N S, ORTH E ORI E S;

IMPORTA N T OBJ E CTIVE :

TO DE VE LOPI N G SP E CIFIC SKILLS,COMP E TEN CI E S, AND POI N T OF VI E W needed by professionals in the closely relatedfields.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 3/59

Concurrency Control & Recovery Very valuable properties of DBMSs without these, DBMSs would be much less useful

Based on concept of transactions with ACIDproperties

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 4/59

Concurrent Execution & Transactions

Concurrent execution essential for goodperformance. Because disk accesses are frequent, and relatively slow,

it is important to keep the CPU humming by working onseveral user programs concurrently. A program may carry out many operations on thedata retrieved from the database, but the DBMS isonly concerned about what data is read/writtenfrom/to the database.transaction - DBMS s abstract view of a userprogram: a sequence of reads and writes.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 5/59

Goal: The ACID properties

A A tomicity: All actions in the transaction happen, or nonehappen.

CC onsistency: If each transaction is consistent, and the DBstarts consistent, it ends up consistent.

II solation: E xecution of one transaction is isolated from thatof all others.

DD urability: If a transaction commits, its effects persist.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 6/59

Atomicity of Transactions

A transaction might commit after completing all itsactions, or it could abort (or be aborted by the DBMS)after executing some actions.

Atomic Transactions : a user can think of atransaction as always either executing all its actions ,or not executing any actions at all .

One approach: DBMS logs all actions so that it can undothe actions of aborted transactions. Another approach: Shadow Pages Logs won because of need for audit trail and for efficiency

reasons.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 7/59

Transaction Consistency

Consistency - data in DBMS is accurate inmodeling real world, follows integrity constraintsUser must ensure transaction consistent by itself If DBMS is consistent before transaction, it will beafter also.

System checks ICs and if they fail, the transactionrolls back (i.e., is aborted).

DBMS enforces some ICs, depending on the ICsdeclared in CREATE TABLE statements.

Beyond this, DBMS does not understand the semanticsof the data. (e.g., it does not understand how the

interest on a bank account is computed).

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 8/59

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 9/59

Durability - Recovering From a CrashSystem Crash - short-term memory lost (disk okay) This is the case we will handle.Disk Crash - stable data lost ouch --- need back ups; raid-techniques can help avoid this.There are 3 phases in Aries recovery (and most others): Analysis: Scan the log forward (from the most recent checkpoint ) to

identify all Xacts that were active, and all dirty pages in the buffer poolat the time of the crash.

Redo: Redoes all updates to dirty pages in the buffer pool, as needed,to ensure that all logged updates are in fact carried out.

Undo: The writes of all Xacts that were active at the crash are undone(by restoring the before value of the update, as found in the log),working backwards in the log.

At the end --- all committed updates and only those updates arereflected in the database. Some care must be taken to handle the case of a crash occurring during

the recovery process!

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 10/59

Plan of attack (ACID properties)

F irst we ll deal with I , by focusing onconcurrency control .

Then we ll address A and D by looking atrecovery .

What about C ?Well, if you have the other three working, and youset up your integrity constraints correctly, then youget this for free (!?).

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 11/59

ExampleConsider two transactions (Xacts):

T1: BEGIN A=A+100, B=B-100 ENDT2: BEGIN A=1.06*A, B=1.06*B END

1st xact transfers $100 from B·s account to A·s2nd credits both accounts with 6% interest.Assume at first A and B each have $1000. Whatare the legal outcomes of running T1 and T2???

$2000 *1.06 = $2120There is no guarantee that T1 will execute beforeT2 or vice-versa, if both are submitted together.But, the net effect must be equivalent to these twotransactions running serially in some order.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 12/59

Example (Contd.)Legal outcomes: A=1166,B=954 or A=1160,B=960Consider a possible interleaved schedule :

T1: A=A+100, B=B-100T2: A=1.06*A, B=1.06*B

This is OK (same as T1;T2). But what about:T1: A=A+100, B=B-100T2: A=1.06*A, B=1.06*B

R esult: A=1166, B=960; A+B = 2126, bank loses $6The DBMS s view of the second schedule:

T1: R(A), W(A), R(B), W(B)T2: R(A), W(A), R(B), W(B)

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 13/59

Scheduling Transactions

S erial schedule: A schedule that does not interleavethe actions of different transactions. i.e., you run the transactions serially (one at a time)

Equivalent schedules: For any database state, theeffect (on the set of objects in the database) andoutput of executing the first schedule is identical to theeffect of executing the second schedule.

S erializable schedule: A schedule that is equivalent tosome serial execution of the transactions. Intuitively: with a serializable schedule you only see things

that could happen in situations where you were runningtransactions one-at-a-time.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 14/59

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 15/59

Conflict Serializable SchedulesW e need a formal notion of equivalence that can beimplemented efficientlyTwo operations conflict if they are by differenttransactions, they are on the same object, and at leastone of them is a write.

Two schedules are conflict equivalent iff:They involve the same actions of the same transactions, andevery pair of conflicting actions is ordered the same way

Schedule S is conflict serializable if S is conflictequivalent to some serial schedule.N ote, some serializable schedules are N OT conflictserializable. This is the price we pay for efficiency.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 16/59

Conflict Equivalence - Intuition

If you can transform an interleaved schedule byswapping consecutive non-conflicting operationsof different transactions into a serial schedule,then the original schedule is conflict serializable .Example:

R(A) R(B)W(A) W(B)

R(A) W(A) R(B)W(B)W(A)

R(B)R(B)

R(A)

W(B)

W(A)

W(B)

R(A)

R(A) R(B)W(A) W(B)R(A) W(A) R(B)W(B)

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 17/59

Conflict Equivalence (Continued)

H ere s another example:

Serializable or not????

R(A) W(A)R(A) W(A)

NOT!

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 18/59

Dependency GraphD ependency graph : One node per Xact; edgefrom Ti to Tj if an operation of Ti conflicts withan operation of Tj and Ti s operation appearsearlier in the schedule than the conflictingoperation of Tj.

Theorem: Schedule is conflict serializable if and only if its dependency graph is acyclic

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 19/59

Example

A schedule that is not conflict serializable:

The cycle in the graph reveals the problem.The output of T1 depends on T2, and vice-versa.

T1 T2A

B

Dependency gra ph

T1: R(A), W(A), R(B), W(B)T2:T1: R(A), W(A), R(B), W(B)T2: R(A), W(A), R(B), W(B)T1: R(A), W(A), R(B), W(B)T2: R(A), W(A), R(B), W(B)T1: R(A), W(A), R(B), W(B)T2: R(A), W(A), R(B), W(B)T1: R(A), W(A), R(B), W(B)T2: R(A), W(A), R(B), W(B)

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 20/59

View Serializability an Aside Alternative (weaker) notion of serializability.Schedules S1 and S2 are view equivalent if: If Ti reads initial value of A in S1, then Ti also reads

initial value of A in S2 If Ti reads value of A written by Tj in S1, then Ti also

reads value of A written by Tj in S2 If Ti writes final value of A in S1, then Ti also writes

final value of A in S2Basically, allows all conflict serializable schedules+ blind writes

T1: R(A) W(A)T2: W(A)

T3: W(A)

T1: R(A),W(A)T2: W(A)

T3: W(A)

view

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 21/59

N otes on Conflict Serializability

Conflict Serializability doesn t allow all schedulesthat you would consider correct. This is because it is strictly syntactic - it doesn t

consider the meanings of the operations or the data.

In practice, Conflict Serializability is what getsused, because it can be done efficiently. N ote: in order to allow more concurrency, some

special cases do get implemented, such as for travelreservations, etc.

Two-phase locking (2PL) is how we implement it.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 22/59

LocksW e use locks to control access to items.

Shared (S) locks multiple transactions canhold these on a particular item at the sametime.

E xclusive (X) locks only one of these and noother locks, can be held on a particular item ata time.

S X

S

X

L ockCompatibilityMatrix

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 23/59

Two-Phase Locking (2PL)

Locking Protocol Each transaction must obtain

a S (shared) or an X (exclusive) lock on object beforereading,an X (exclusive) lock on object before writing.

A transaction can not request additional locksonce it releases any locks.

Thus, there is a growing phase followed by a shrinking phase .2PL on its own is sufficient to guaranteeconflict serializability. Doesn t allow cycles in the dependency graph!

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 24/59

Lock_X(A) <granted>R ead(A) Lock_S(A)

A: = A-50W rite(A)

Unlock(A) <granted>R ead(A)

Unlock(A)Lock_S(B) <granted>

Lock_X(B)R ead(B)

<granted> Unlock(B)

P R I N T(A+B)R ead(B)B := B +50W rite(B)Unlock(B)

Ex 1: A= 1000, B=2000, Output =?

Is it a 2PL schedule? No, and it is not serializable.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 25/59

Lock_X(A) <granted>R ead(A) Lock_S(A)

A: = A-50W rite(A)

Lock_X(B) <granted>Unlock(A) <granted>

R ead(A)Lock_S(B)

R ead(B)B := B +50W rite(B)

Unlock(B) <granted>Unlock(A)R ead(B)Unlock(B)P R I N T(A+B)

Ex 2: A= 1000, B=2000, Output =?

Is it a 2PL schedule? Yes ± so it is serializable.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 26/59

P roblem with 2 P L: Cascading AbortsE xample: rollback of T1 requires rollback of T2!

Avoiding Cascading Aborts Strict 2PL

Solution: Strict Two-phase Locking (Strict 2 P L): Same as 2PL, except: All locks held by a transaction are released only

when the transaction completes

T1: R(A), W(A), R(B), W(B) Q: is itT2: R(A), W(A) 2PL ?T1: R(A), W(A), R(B), W(B), AbortT2: R(A), W(A)

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 27/59

Strict 2PL (continued)

All locks held by a transaction are released onlywhen the transaction completesLike 2 P L, Strict 2 P L allows only scheduleswhose precedence graph is acyclic, but it isactually stronger than needed for thatpurpose.

In effect, shrinking phase is delayed until:a) Transaction has committed (commit log record

on disk), orb) Decision has been made to abort the transaction

(then locks can be released after rollback).

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 28/59

Lock_X(A) <granted>R ead(A) Lock_S(A)

A: = A-50W rite(A)

Lock_X(B) <granted>R ead(B)

B := B +50W rite(B)Unlock(A)Unlock(B) <granted>

R ead(A)

Lock_S(B) <granted>R ead(B)PR I N T(A+B)Unlock(A)Unlock(B)

Ex 3: A= 1000, B=2000, Output =?

Is it a 2PL schedule? Strict 2PL?

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 29/59

Lock_X(A) <granted>R ead(A) Lock_S(A)

A: = A-50W rite(A)

Lock_X(B) <granted>Unlock(A) <granted>

R ead(A)Lock_S(B)

R ead(B)B := B +50W rite(B)

Unlock(B) <granted>Unlock(A)R ead(B)Unlock(B)PR I N T(A+B)

Ex 2: Revisited

Is it Strict 2PL? No: Cascading Abort Poss.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 30/59

Lock Management Lock and unlock requests are handled by the Lock

Manager.LM contains an entry for each currently held lock.Lock table entry: Ptr. to list of transactions currently holding the lock Type of lock held (shared or exclusive) Pointer to queue of lock requestsW hen lock request arrives see if anyone else holding aconflicting lock. If not, create an entry and grant the lock. Else, put the requestor on the wait queueLocking and unlocking have to be atomic operationsLock upgrade: transaction that holds a shared lock canbe upgraded to hold an exclusive lock Can cause deadlock problems

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 31/59

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 32/59

Deadlocks

Deadlock: Cycle of transactions waiting forlocks to be released by each other.

Two ways of dealing with deadlocks: Deadlock prevention Deadlock detection

Many systems just punt and use Timeouts What are the dangers with this approach?

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 33/59

Deadlock Prevention

Assign priorities based on timestamps. AssumeTi wants a lock that Tj holds. Two policies arepossible: Wait-Die: If Ti is older, Ti waits for Tj; otherwise

Ti aborts Wound-wait: If Ti is older, Tj aborts; otherwise Ti

waitsIf a transaction re-starts, make sure it gets itsoriginal timestamp Why?

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 34/59

Deadlock Detection

Alternative is to allow deadlocks to happen butto check for them and fix them if found.

Create a waits-for graph : N odes are transactions There is an edge from Ti to Tj if Ti is waiting for Tj

to release a lockP eriodically check for cycles in the waits-forgraph

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 35/59

Deadlock Detection (Continued)E xample:

T1: S(A), S(D), S(B)T2: X(B) X(C)T3: S(D), S(C), X(A)T4: X(B)

T1 T2

T4 T3

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 36/59

Multiple-Granularity LocksH ard to decide what granularity to lock (tuplesvs. pages vs. tables).Shouldn t have to make same decision for alltransactions!Data containers are nested:

Tuples

Tables

Pages

Database

contains

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 37/59

Solution: N ew Lock Modes, Protocol Allow Xacts to lock at each level, but with aspecial protocol using new intention locks :Still need S and X locks, but before locking anitem, Xact must have proper intension locks onall its ancestors in the granularity hierarchy.

IS ² Intent to get S lock(s) atfiner granularity.IX ² Intent to get X lock(s)at finer granularity.SIX mode: L ike S & IX at

the same time. Why useful?

IS IX SIX

IS

IXSIX

S X

S

X

-

-

--

-

--

-

-

--

---

-

-

Tuples

Tables

Pages

Database

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 38/59

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 39/59

Examples 2 level hierarchyT1 scans R, and updates a few tuples: T1 gets an SIX lock on R, then get X lock on

tuples that are updated.T2 uses an index to read only part of R : T2 gets an IS lock on R, and repeatedly

gets an S lock on tuples of R.T3 reads all of R : T3 gets an S lock on R. OR, T3 could behave like T2; can

use lock escalation to decide which. Lock escalation dynamically asks for

courser-grained locks when too manylow level locks acquired

IS IX SIX

ISIXSIX

S X

SX

Tuples

Tables

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 40/59

Dynamic Databases The PhantomProblem

If we relax the assumption that the DB is a fixedcollection of objects , even Strict 2 P L (on individualitems) will not assure serializability:Consider T1 Find oldest sailor

T1 locks all records, and finds oldest sailor (say, age =71).

N ext, T2 inserts a new sailor; age = 96 and commits. T1 (within the same transaction) checks for the oldest

sailor again and finds sailor aged 96!!The sailor with age 96 is a phantom tuple fromT1 s point of view --- first it s not there then it is.N o serial execution where T1 s result could happen!

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 41/59

The Phantom Problem example 2

Consider T3 Find oldest sailor for each rating T3 locks all pages containing sailor records with rating =

1, and finds oldest sailor (say, age = 71). N ext, T4 inserts a new sailor; rating = 1, age = 96.

T4 also deletes oldest sailor with rating = 2 (and, say,age = 80), and commits.

T3 now locks all pages containing sailor records withrating = 2, and finds oldest (say, age = 63).

T3 saw only part of T4 s effects!N o serial execution where T3 s result could happen!

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 42/59

The Problem

T1 and T3 implicitly assumed that they hadlocked the set of all sailor records satisfying apredicate. Assumption only holds if no sailor records are

added while they are executing! N eed some mechanism to enforce this

assumption. (Index locking and predicatelocking.)

E xamples show that conflict serializability onreads and writes of individual itemsguarantees serializability only if the set of objects is fixed!

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 43/59

Predicate Locking (not practical)

G rant lock on all records that satisfy somelogical predicate , e.g. age > 2*salary .

Index locking is a special case of predicatelocking for which an index supports efficientimplementation of the predicate lock. What is the predicate in the sailor example?In general , predicate locking has a lot of locking overhead.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 44/59

Index LockingIf there is a dense index on the rating fieldusing Alternative (2) , T3 should lock the indexpage containing the data entries with rating =

1. If there are no records with rating = 1, T3 must lock the index page where such a data entrywould be, if it existed!

If there is no suitable index , T3 must lock allpages , and lock the file/table to prevent newpages from being added , to ensure that norecords with rating = 1 are added or deleted.

r=1

DataIndex

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 45/59

Transaction Support in SQL-92

SE R IALIZABLE N o phantoms , all readsrepeatable , no dirty (uncommited) reads.R E P E

ATABLE

R E

ADS phantoms may happen.R E AD COMMITT E D phantoms andunrepeatable reads may happenR E AD U N COMMITTE D all of them mayhappen.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 46/59

Optimistic CC (Kung-Robinson)

Locking is a conservative approach inwhich conflicts are prevented.Disadvantages:Lock management overhead.Deadlock detection/resolution.Lock contention for heavily used objects.

Locking is pessimistic because itassumes that conflicts will happen.

If conflicts are rare , we might get betterperformance by not locking , and insteadchecking for conflicts at commit.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 47/59

Kung-Robinson Model

Xacts have three phases:

READ: Xacts read from the database, but

make changes to private copies of objects. V ALIDATE: Check for conflicts. WRITE: Make local copies of changes

public.

ROOT

old

newmodifiedobjects

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 48/59

Details on Opt. CC

Students not responsible for the followingalgorithmic details for this class

But you need to understand the basicdifferences between pessimistic and optimistic approaches.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 49/59

Validation

Test conditions that are sufficient to ensurethat no conflict occurred.E

ach Xact is assigned a numeric id. Just use a timestamp .Xact ids assigned at end of R E AD phase , justbefore validation begins.R

eadSet(Ti): Set of objects read by Xact Ti.W riteSet(Ti): Set of objects modified by Ti.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 50/59

Test 1

For all i and j such that Ti < Tj , check that Ticompletes before Tj begins.

TiTjR V W

R V W

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 51/59

Test 2For all i and j such that Ti < Tj , check that:

Ti completes before Tj begins its Write phase

and WriteSet(Ti) ReadSet(Tj) is empty.

Ti

Tj

R V W

R V W

Does Tj read dirty data? Does Ti overwrite Tj·s writes?

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 52/59

Test 3For all i and j such that Ti < Tj , check that:

Ti completes Read phase before Tj does +

WriteSet(Ti) ReadSet(Tj) is empty +

WriteSet(Ti) WriteSet(Tj) is empty.Ti

TjR V W

R V W

Does Tj read dirty data? Does Ti overwrite Tj·s writes?

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 53/59

Applying Tests 1 & 2: Serial ValidationTo validate Xact T:

valid = true;// S = set of Xacts that committed after Begin(T)// (above defn implements Test 1)

//The following is done in critical section< fo r each Ts in S do {if ReadSet(T) intersects WriteSet(Ts)

then valid = false;

}if valid then { install updates; // Write phase

Commit T }>else Restart T

sta r tofcr iticalsection

end of c r itical section

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 54/59

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 55/59

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 56/59

Other Techniques

Timestamp CC:G

ive each object a read-timestamp ( R TS) anda write-timestamp ( W TS) , give each Xact a timestamp (TS)when it begins:

If action ai of Xact Ti conflicts with action aj of Xact Tj, and TS(Ti) < TS(Tj), then ai must occur before aj.

Otherwise, restart violating Xact.Multiversion CC: Let writers make a new copy while readersuse an appropriate old copy. Advantage is that readers don t need to get locks Oracle uses a simple form of this.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 57/59

SummaryCorrectness criterion for isolation is serializability .

In practice, we use conflict serializability , which is somewhat morerestrictive but easy to enforce.Two P hase Locking , and Strict 2 P L: Locks directly implementthe notions of conflict. The lock manager keeps track of the locks issued. Deadlocks can

either be prevented or detected.Must be careful if objects can be added to or removed from thedatabase ( phantom problem ).Index locking common , affects performance significantly. N eeded when accessing records via index. N eeded for locking logical sets of records (index locking/predicate

locking).

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 58/59

Summary (Contd.)

Multiple granularity locking reduces the overheadinvolved in setting locks for nested collections of objects (e.g. , a file of pages);

should not be confused with tree index locking!Tree-structured indexes: Straightforward use of 2PL very inefficient. Idea is to use 2PL on data to ensure serializability and use

other protocols on tree to ensure structural integrity. We didn t cover this, but if you re interested, it s in the book.

8/8/2019 Chap16 17 Transaction Con Currency

http://slidepdf.com/reader/full/chap16-17-transaction-con-currency 59/59

Summary (Contd.)

Optimistic CC aims to minimize CC overheads in an``optimistic environment where reads are common andwrites are rare.Optimistic CC has its own overheads however; most realsystems use locking.There are many other approaches to CC that we don t coverhere. These include: timestamp-based approaches multiple-version approaches semantic approaches