29
Transaction

Transaction unit1 topic 2

  • Upload
    avnis

  • View
    440

  • Download
    2

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Transaction unit1 topic 2

Transaction

Page 2: Transaction unit1 topic 2
Page 3: Transaction unit1 topic 2

Transaction definition• Supports daily operations of an organization• Collection of database operations• Reliably and efficiently processed as one unit

of work• No lost data– Interference among multiple users– Failures

Page 4: Transaction unit1 topic 2

Airline Transaction Example• START TRANSACTION• Display greeting• Get reservation preferences from user• SELECT departure and return flight records• If reservation is acceptable then• UPDATE seats remaining of departure flight

record• UPDATE seats remaining of return flight record• INSERT reservation record• Print ticket if requested• End If• On Error: ROLLBACK • COMMIT

Page 5: Transaction unit1 topic 2

ATM transaction example• START TRANSACTION• Display greeting• Get account number, pin, type, and amount• SELECT account number, type, and balance• If balance is sufficient then• UPDATE account by posting debit• INSERT history record• Display message and dispense cash• Print receipt if requested• End If• On Error: ROLLBACK • COMMIT

Page 6: Transaction unit1 topic 2
Page 7: Transaction unit1 topic 2

Transaction Properties• Atomic: all or nothing• Consistent: database must be consistent

before and after a transaction• Isolated: no unwanted interference from

other users• Durable: database changes are permanent

after the transaction completes

Page 8: Transaction unit1 topic 2
Page 9: Transaction unit1 topic 2
Page 10: Transaction unit1 topic 2
Page 11: Transaction unit1 topic 2
Page 12: Transaction unit1 topic 2
Page 13: Transaction unit1 topic 2

Serializable schedule• A serializable schedule is a linear

arrangement of the database calls from several transactions with the property: the final database state obtained by executing the calls in schedule order is the same as that obtained by running the transactions in some unspecified serial order.

Page 14: Transaction unit1 topic 2

Serializability thru lock

• A lock is an access privilege on a database object, which the DBMS grant to a particular transaction

• Exclusive locks prevent any current access. A shared lock lets you read an object, but you need an exclusive lock to update it.

Page 15: Transaction unit1 topic 2
Page 16: Transaction unit1 topic 2
Page 17: Transaction unit1 topic 2

Transaction Management with SQL• Transaction support is provided by two SQL

statements: COMMIT and ROLLBACK.• A COMMIT statement is reached, in which

case all changes are permanently recorded within the database. The COMMIT statement automatically ends the SQL transaction.

• A ROLLBACK statement is reached in which case all changes are aborted and the database is rolled back to its previous consistent state.

Page 18: Transaction unit1 topic 2
Page 19: Transaction unit1 topic 2
Page 20: Transaction unit1 topic 2
Page 21: Transaction unit1 topic 2
Page 22: Transaction unit1 topic 2
Page 23: Transaction unit1 topic 2

Serializability Testing and Precedence Graphs/Dependency Graph

• So we need a simple method to test a schedule S and discover whether it is serializable.

• Simple method involves constructing a directed graph called a Precedence Graph from S

• Construct a precedence graph as follows:– a vertex labelled Ti for every transaction in S– an edge from Ti to Tj if any of these three conditions holds:

• Ti executes write(Q) before Tj executes read(Q)• Ti executes read(Q) before Tj executes write(Q)• Ti executes write(Q) before Tj executes write(Q)

– if the graph has a cycle, S is not serializable

Page 24: Transaction unit1 topic 2

• Compute a precedence graph for schedule B (right)

• three vertices (T1, T2, T3)

• edge from Ti to Tj if– Ti writes Q before Tj reads Q – Ti reads Q before Tj writes Q (T1-T2)– Ti writes Q before Tj writes Q

Page 25: Transaction unit1 topic 2
Page 26: Transaction unit1 topic 2

Examples of Conflicts• A conflict exists when two transactions access the same item, and at least

one of the accesses is a write.• 1. lost update problem

• T: transfer $100 from A to C: R(A) W(A) R(C) W(C)• S: transfer $100 from B to C: R(B) W(B) R(C) W(C)

• 2. inconsistent retrievals problem (dirty reads violate consistency)• T: transfer $100 from A to C: R(A) W(A) R(C) W(C)• S: compute total balance for A and C: R(A) R(C)

• 3. nonrepeatable reads• T: transfer $100 from A to C: R(A) W(A) R(C)

W(C)• S: check balance and withdraw $100 from A: R(A) R(A) W(A)

Page 27: Transaction unit1 topic 2

Serializable schedule• A schedule is a partial ordering of operations for a set of transactions

{T,S,...}, such that:– The operations of each action execute serially.– The schedule specifies an order for conflicting operations.

• Any two schedules for {T,S,...} that order the conflicting operations in the same way are equivalent.

• A schedule for {T,S,...} is serializable if it is equivalent to some serial schedule on {T,S,...}.

• There may be other serializable schedules on {T,S,...} that do not meet this condition, but if we enforce this condition we are safe.

• Conflict serializability: detect conflicting operations and enforce a serial-equivalent order.

Page 28: Transaction unit1 topic 2

Legal Interleaved schedule1. avoid lost update problem

• T: transfer $100 from A to C: R(A) W(A) R(C) W(C)

• S: transfer $100 from B to C: R(B) W(B) R(C) W(C)

2. avoid inconsistent retrievals problem • T: transfer $100 from A to C: R(A) W(A) R(C) W(C)

• S: compute total balance for A and C: R(A) R(C)

3. avoid non-repeatable reads

T: transfer $100 from A to C R(A) W(A) R(C) W(C)

S: check balance and withdraw $100 from A: R(A) R(A) W(A)

Page 29: Transaction unit1 topic 2

Defining legal schedule• . To be serializable, the conflicting operations of T and S must be

ordered as if either T or S had executed first.• We only care about the conflicting operations:

everything else will take care of itself.• 2. Suppose T and S conflict over some shared item(s) x.• 3. In a serial schedule, T’s operations on x would appear before S’s,

or vice versa....for every shared item x.• As it turns out, this is true for all the operations, but

again, we only care about the conflicting ones.• 4. A legal (conflict-serializable) interleaved schedule of T and S must

exhibit the same property.• Either T or S “wins” in the race to x; serializability

dictates that the “winner take all”.