databases: concurrency control

25
Concurrency Control

Upload: damian-gordon

Post on 19-Jun-2015

2.648 views

Category:

Documents


4 download

TRANSCRIPT

  • 1. Concurrency Control

2. Concurrency Control

  • Transaction
    • the basic unit of work in a DBMS
  • Properties of a transaction
    • ATOMICITY
    • CONSISTENCY
    • INDEPENDENCE
    • DURABILITY

3. A.C.I.D properties (1)

  • Atomicity
    • the is the all or nothing property ; a transaction is an indivisible unit of work

4. A.C.I.D properties (1)

  • Consistency
    • transactions transform the DB from one consistent state to another consistence state

5. A.C.I.D properties (2)

  • Independence
    • transactions execute independently of one another i.e. the partial effect of one transaction is not visible to other transactions.

6. A.C.I.D properties (2)

  • Durability (aka Persistence)
    • the effect of a successfully completed (i.e. committed) transaction are permanently recorded in the DB and cannot be undone.

7. Example Transaction

  • Funds transfer :
  • begin transaction T1
  • read balance1
  • balance1 = balance1 - 100
  • if balance1 < 0
    • then print insufficient funds
    • abort T1
  • end
  • write balance1
  • read balance2
  • balance2 = balance2 + 100
  • write balance2
  • commit T1

8. Discussing the Example

  • Effect of the abort is to rollback the transaction and undo changes it has made on the DB
  • in this example, transaction was not written to the DB prior to abort and so no undo is necessary

9. Problems with Concurrency

  • Concurrent transaction can cause three kinds of database problems
    • Lost Update
    • Violation of Integrity Constraints
    • Inconsistent Retrieval

10. Lost Update

  • Apparently successful updates can be overwritten be other transactions

Begin transaction T1 read balance [ 100 ] balance = balance - 100if balance < 0 print insufficient funds abort T1 end write balance [ 0 ] Initial Balance= 100 Begin transaction T2 read balance [ 100 ] balance = balance + 100 write balance [ 200 ] commit T2 11. Violation of Integrity Constraints 12. Violation of Integrity Constraints

  • Begin transaction T3
  • read schedule where date = 4/4/01
  • read surgeon where surgeon.name = schedule.surgeon and surgeon.operation = Appendectomy
  • if not found then abort T3
  • schedule.operation = Appendectomy
  • commit T3
  • Begin transaction T4
  • read schedule where date = 4/4/01
  • read surgeon where surgeon.name = Tom
  • if not found then abort T4
  • surgeon.surgeon = Tom
  • commit T4

13. Violation of Integrity Constraints 14. Inconsistent Retrieval ( Dirty Reads )

  • Most concurrency control systems focus on the transaction which update the DB since they are the only ones which can corrupt the DB
  • If transaction are allowed toreadthe partial results of incomplete transactions, they can obtain an inconsistent view of the DB ( dirtyorunrepeatable reads )

15. Inconsistent Retrieval ( Dirty Reads )

  • T1
  • begin transaction T1
  • read BalanceX
  • BalanceX = BalanceX - 100
  • if BalanceX < 0 then
  • begin
  • print insufficient funds
  • abort T1
  • end
  • write BalanceX
  • read BalanceY
  • BalanceY = BalanceY + 100
  • write BalanceY
  • commit T1
  • T2
  • begin transaction T2
  • read BalanceX
  • :
  • :
  • :
  • :
  • :
  • :
  • :
  • :
  • :
  • :
  • :
  • :
  • read BalanceY
  • commit T2

16. Concurrency Control

  • Schedules and Serialisation
  • Order in a schedule is VERY important
  • S = [R1(x), R2(x), W1(x), W2(x)]
  • so, is it O.K. to do reads before or after writes, e.g. Lost Update Problem

17. Conflicting Operations

  • If two transactions only read a data item, they donotconflict and order is not important.
  • If two transactions either read or write completely separate data items, they donotconflict and order is not important.
  • If one transactions writes a data item and another transaction reads or writes the same data item, the order of executionisimportant.

18. Serial Schedule

  • What is a serial schedule ?
  • S = [R1(X), W1(X), R2(X), W2(X), R3(X)]

19. Serialisable Schedule

  • What is a serialisable schedule ?
  • S = [R1(x), R2(x),W1(x), R3(x), W2(x)]
  • Is this serialisable ?

20. General Solution

  • Constrained Write Rule
  • Transaction should always Read before they Write

21. Rules for Equivalence of Schedules

  • Each read operation must read the same values in both schedules; this effectively means that those values must have been produced by the same write operation the both schedules
  • The final state of the database must be the same in both schedules; thus the final write operation on each data item is the same in both schedules

22. Try these...

  • [R1(x), W1(x), R2(x), W2(x)]
  • [R1(x), R2(x), W1(x), W2(x)]
  • [R1(x), R3(y), R2(x), W2(z), W2(y), W1(x), R2(y), W1(z)]

23. Conflicting Operations

  • Read operations cannot conflict with one and other, thus the order of read operations does not matter.
  • i.e.[R1(x), R2(x)] = [R2(x), R1(x)]
  • but
  • [R1(x),W1(x),R2(x)] != [R1(x), R2(x), W1(x)]

24. Conflicting Operations

  • In terms of schedule equivalence, it is the ordering of CONFLICTING operators which must be the same in both schedules
  • The conflict between read and write operations is called aread-write conflictand the conflict between two writes is called awrite-write conflict .

25. Concurrency Control Techniques

  • There are three basic concurrency control techniques :
  • Locking Methods
  • Timestamp Methods
  • Optimistic Methods