Transac'ons 1 Why Transac'ons? Database systems are - - PowerPoint PPT Presentation

transac ons
SMART_READER_LITE
LIVE PREVIEW

Transac'ons 1 Why Transac'ons? Database systems are - - PowerPoint PPT Presentation

Transac'ons 1 Why Transac'ons? Database systems are normally being accessed by many users or processes at the same 'me. Both queries and


slide-1
SLIDE 1

Transac'ons ¡

1 ¡

slide-2
SLIDE 2

Why ¡Transac'ons? ¡

  • Database ¡systems ¡are ¡normally ¡being ¡

accessed ¡by ¡many ¡users ¡or ¡processes ¡at ¡the ¡ same ¡'me. ¡

– Both ¡queries ¡and ¡modifica'ons. ¡

  • Unlike ¡opera'ng ¡systems, ¡which ¡support ¡ ¡

interac'on ¡of ¡processes, ¡a ¡DMBS ¡needs ¡to ¡ keep ¡processes ¡from ¡troublesome ¡ interac'ons. ¡

2 ¡

slide-3
SLIDE 3

Transac'ons ¡

  • A ¡single ¡"unit ¡of ¡work" ¡in ¡a ¡DBMS. ¡
  • Can ¡comprise ¡more ¡than ¡one ¡SQL ¡command, ¡

but ¡each ¡individual ¡command ¡does ¡not ¡stand ¡

  • n ¡its ¡own. ¡

3 ¡

slide-4
SLIDE 4

Statement ¡of ¡Problem ¡

  • How ¡do ¡we ¡allow ¡concurrent ¡running ¡of ¡

independent ¡transac'ons ¡while ¡preserving ¡ database ¡integrity? ¡

  • Addi'onally, ¡we ¡want ¡ ¡

– good ¡response ¡'me ¡and ¡minimal ¡wai'ng. ¡ – correctness ¡and ¡fairness. ¡

5 ¡

slide-5
SLIDE 5

6 ¡

slide-6
SLIDE 6

Another ¡example: ¡"lost ¡update" ¡ problem ¡

T1 Read(N) T2 Read(N) N=N-1 N= N-1 Write(N) Write(N)

time

9 ¡

slide-7
SLIDE 7

Concurrency ¡

  • Arbitrary ¡interleaving ¡can ¡lead ¡to ¡ ¡

– Temporary ¡inconsistency ¡(unavoidable) ¡ – "Permanent" ¡inconsistency ¡(bad!) ¡

10 ¡

slide-8
SLIDE 8

Example: ¡Bad ¡Interac'on ¡

  • You ¡and ¡friend ¡each ¡take ¡$100 ¡from ¡different ¡

ATMs ¡at ¡about ¡the ¡same ¡'me. ¡

– The ¡DBMS ¡had ¡be]er ¡make ¡sure ¡one ¡account ¡ deduc'on ¡doesn’t ¡get ¡lost. ¡

  • Compare: ¡An ¡OS ¡allows ¡two ¡people ¡to ¡edit ¡a ¡

document ¡at ¡the ¡same ¡'me. ¡ ¡If ¡both ¡write, ¡

  • ne’s ¡changes ¡get ¡lost. ¡

11 ¡

slide-9
SLIDE 9

Remember ¡ACID? ¡

12 ¡

slide-10
SLIDE 10

Remember ¡ACID? ¡

13 ¡

slide-11
SLIDE 11

ACID ¡Transac'ons ¡

  • We ¡want ¡transac+ons ¡to ¡be: ¡

– Atomic: ¡Whole ¡transac'on ¡or ¡none ¡is ¡done. ¡ – Consistent: ¡Database ¡constraints ¡preserved. ¡ – Isolated: ¡It ¡appears ¡to ¡the ¡user ¡as ¡if ¡only ¡one ¡ transac'on ¡executes ¡at ¡a ¡'me. ¡ – Durable: ¡Effects ¡of ¡a ¡transac'on ¡survive ¡a ¡crash. ¡

14 ¡

slide-12
SLIDE 12

SQL ¡Transac'ons ¡

  • BEGIN ¡TRANSACTION ¡
  • // ¡do ¡SQL ¡here ¡
  • either ¡COMMIT ¡or ¡ROLLBACK ¡

15 ¡

slide-13
SLIDE 13

COMMIT ¡

  • The ¡SQL ¡statement ¡COMMIT ¡causes ¡a ¡

transac'on ¡to ¡complete. ¡

– Any ¡database ¡modifica'ons ¡are ¡now ¡permanent ¡in ¡ the ¡database. ¡

16 ¡

slide-14
SLIDE 14

ROLLBACK ¡

  • The ¡SQL ¡statement ¡ROLLBACK ¡also ¡causes ¡the ¡

transac'on ¡to ¡end, ¡but ¡by ¡abor3ng. ¡

– No ¡effects ¡on ¡the ¡database. ¡

  • Failures ¡like ¡division ¡by ¡0 ¡or ¡a ¡constraint ¡

viola'on ¡can ¡also ¡cause ¡rollback, ¡even ¡if ¡the ¡ programmer ¡does ¡not ¡request ¡it. ¡

17 ¡

slide-15
SLIDE 15

Isola'on ¡Levels ¡

  • SQL ¡defines ¡four ¡isola3on ¡levels: ¡choices ¡about ¡

what ¡interac'ons ¡are ¡allowed ¡by ¡transac'ons ¡ that ¡execute ¡at ¡about ¡the ¡same ¡'me. ¡

  • Only ¡one ¡level ¡(serializable) ¡gives ¡ACID ¡

transac'ons. ¡

  • Each ¡DBMS ¡implements ¡transac'ons ¡in ¡its ¡own ¡
  • way. ¡
  • Not ¡all ¡DBMS ¡implement ¡all ¡four ¡isola'on ¡levels. ¡

18 ¡

slide-16
SLIDE 16

Let's ¡get ¡abstract ¡

  • database ¡-­‑ ¡a ¡fixed ¡set ¡of ¡named ¡data ¡objects ¡

(A, ¡B, ¡C, ¡…) ¡

  • transac'on ¡-­‑ ¡a ¡sequence ¡of ¡read ¡and ¡write ¡
  • pera'ons ¡(read(A), ¡write(B), ¡…) ¡

– DBMS's ¡abstract ¡view ¡of ¡a ¡user ¡program ¡

20 ¡

slide-17
SLIDE 17

ACID ¡Transac'ons ¡

  • ACID ¡transac+ons ¡ ¡are: ¡

– Atomic ¡: ¡Whole ¡transac'on ¡or ¡none ¡is ¡done. ¡ – Consistent ¡: ¡Database ¡constraints ¡preserved. ¡ – Isolated ¡: ¡It ¡appears ¡to ¡the ¡user ¡as ¡if ¡only ¡one ¡ process ¡executes ¡at ¡a ¡'me. ¡ – Durable ¡: ¡Effects ¡of ¡a ¡process ¡survive ¡a ¡crash. ¡

21 ¡

slide-18
SLIDE 18

Atomicity ¡of ¡Transac'ons ¡

  • Two ¡possible ¡outcomes ¡of ¡execu'ng ¡a ¡

transac'on: ¡

– Xact ¡might ¡commit ¡aler ¡comple'ng ¡all ¡its ¡ac'ons ¡ – or ¡it ¡could ¡abort ¡(or ¡be ¡aborted ¡by ¡the ¡DBMS) ¡ aler ¡execu'ng ¡some ¡ac'ons. ¡

  • DBMS ¡guarantees ¡that ¡Xacts ¡are ¡atomic. ¡ ¡ ¡

– From ¡user's ¡point ¡of ¡view: ¡Xact ¡always ¡either ¡ executes ¡all ¡its ¡ac'ons, ¡or ¡executes ¡no ¡ac'ons ¡at ¡

  • all. ¡

A

22 ¡

slide-19
SLIDE 19

Mechanisms ¡for ¡Ensuring ¡Atomicity ¡

  • What ¡would ¡you ¡do? ¡

A

24 ¡

slide-20
SLIDE 20

Mechanisms ¡for ¡Ensuring ¡Atomicity ¡

  • One ¡approach: ¡LOGGING ¡

– DBMS ¡logs ¡all ¡ac'ons ¡so ¡that ¡it ¡can ¡undo ¡the ¡ ac'ons ¡of ¡aborted ¡transac'ons. ¡

  • ~ ¡like ¡black ¡box ¡in ¡airplanes ¡… ¡

A

25 ¡

slide-21
SLIDE 21

Mechanisms ¡for ¡Ensuring ¡Atomicity ¡

  • Logging ¡used ¡by ¡all ¡modern ¡systems. ¡ ¡
  • Q: ¡why? ¡

A

26 ¡

slide-22
SLIDE 22

Mechanisms ¡for ¡Ensuring ¡Atomicity ¡

  • Logging ¡used ¡by ¡all ¡modern ¡systems. ¡ ¡
  • Q: ¡why? ¡
  • A: ¡ ¡

– audit ¡trail ¡& ¡ – efficiency ¡reasons ¡

A

27 ¡

slide-23
SLIDE 23

Transac'on ¡Consistency ¡

  • "Database ¡consistency" ¡-­‑ ¡data ¡in ¡DBMS ¡is ¡

accurate ¡in ¡modeling ¡real ¡world ¡and ¡follows ¡ integrity ¡constraints ¡

C

29 ¡

slide-24
SLIDE 24

Transac'on ¡Consistency ¡

  • “Transac'on ¡Consistency”: ¡if ¡DBMS ¡consistent ¡

before ¡Xact ¡(running ¡alone), ¡it ¡will ¡be ¡aler ¡ also ¡

  • Transac'on ¡consistency: ¡User’s ¡responsibility ¡

– DBMS ¡just ¡checks ¡IC ¡ consistent database S1 consistent database S2 transaction T

C

30 ¡

slide-25
SLIDE 25

Transac'on ¡Consistency ¡(cont.) ¡

  • Recall: ¡Integrity ¡constraints ¡

– must ¡be ¡true ¡for ¡DB ¡to ¡be ¡considered ¡consistent ¡ Examples: ¡

  • 1. FOREIGN ¡KEY ¡R.sid ¡REFERENCES ¡S ¡
  • 2. BALANCE ¡>= ¡0 ¡

C

31 ¡

slide-26
SLIDE 26

Transac'on ¡Consistency ¡(cont.) ¡

  • System ¡checks ¡ICs ¡and ¡if ¡they ¡fail, ¡the ¡

transac'on ¡rolls ¡back ¡(i.e., ¡is ¡aborted). ¡

– Beyond ¡this, ¡DBMS ¡does ¡not ¡understand ¡the ¡ seman'cs ¡of ¡the ¡data. ¡ – e.g., ¡it ¡does ¡not ¡understand ¡how ¡interest ¡on ¡a ¡ bank ¡account ¡is ¡computed ¡

  • This ¡is ¡the ¡user's ¡responsibility; ¡DB ¡cannot ¡do ¡

much ¡other ¡than ¡enforce ¡the ¡rules ¡and ¡ rollback ¡if ¡violated. ¡

C

32 ¡

slide-27
SLIDE 27

Isola'on ¡of ¡Transac'ons ¡

  • Users ¡submit ¡transac'ons, ¡and ¡ ¡
  • Each ¡transac'on ¡executes ¡as ¡if ¡it ¡was ¡running ¡

by ¡itself. ¡

– Concurrency ¡is ¡achieved ¡by ¡DBMS, ¡which ¡ interleaves ¡ac'ons ¡(reads/writes ¡of ¡DB ¡objects) ¡of ¡ various ¡transac'ons. ¡

  • Q: ¡How ¡would ¡you ¡achieve ¡that? ¡

I

33 ¡

slide-28
SLIDE 28

Isola'on ¡of ¡Transac'ons ¡

  • A: ¡Many ¡methods ¡-­‑ ¡two ¡main ¡categories: ¡
  • Pessimis'c ¡– ¡don’t ¡let ¡problems ¡arise ¡in ¡the ¡

first ¡place ¡

  • Op'mis'c ¡– ¡assume ¡conflicts ¡are ¡rare, ¡deal ¡

with ¡them ¡aler ¡they ¡happen. ¡

I

34 ¡

slide-29
SLIDE 29

Example ¡

  • Consider ¡two ¡transac'ons ¡(Xacts): ¡

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

  • 1st ¡xact ¡transfers ¡$100 ¡from ¡B’s ¡account ¡to ¡A’s ¡
  • 2nd ¡credits ¡both ¡accounts ¡with ¡6% ¡interest. ¡
  • Assume ¡at ¡first ¡A ¡and ¡B ¡each ¡have ¡$1000. ¡ ¡What ¡are ¡

the ¡legal ¡outcomes ¡of ¡running ¡T1 ¡and ¡T2? ¡

I

35 ¡

slide-30
SLIDE 30

Example ¡

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

  • many ¡-­‑ ¡but ¡A+B ¡should ¡be: ¡$2000 ¡*1.06 ¡= ¡$2120 ¡
  • There ¡is ¡no ¡guarantee ¡that ¡T1 ¡will ¡execute ¡before ¡T2 ¡
  • r ¡vice-­‑versa, ¡if ¡both ¡are ¡submi]ed ¡together. ¡ ¡But, ¡the ¡

net ¡effect ¡must ¡be ¡equivalent ¡to ¡these ¡two ¡ transac'ons ¡running ¡serially ¡in ¡some ¡order. ¡

I

36 ¡

slide-31
SLIDE 31

Example ¡(Contd.) ¡

  • Legal ¡outcomes: ¡A=1166,B=954 ¡or ¡A=1160,B=960 ¡
  • Consider ¡a ¡possible ¡interleaved ¡schedule: ¡

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

  • This ¡is ¡OK ¡(same ¡as ¡T1;T2). ¡ ¡But ¡what ¡about: ¡

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

I

37 ¡

slide-32
SLIDE 32

Example ¡(Contd.) ¡

  • Legal ¡outcomes: ¡A=1166,B=954 ¡or ¡A=1160,B=960 ¡
  • Consider ¡a ¡possible ¡interleaved ¡schedule: ¡

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

  • This ¡is ¡OK ¡(same ¡as ¡T1;T2). ¡ ¡But ¡what ¡about: ¡

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

  • Result: ¡A=1166, ¡B=960; ¡A+B ¡= ¡2126, ¡bank ¡loses ¡$6 ¡
  • The ¡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)

I

38 ¡

slide-33
SLIDE 33

Anomalies ¡with ¡Interleaved ¡Execu'on ¡

  • Reading ¡uncommi]ed ¡data ¡(WR ¡Conflicts, ¡"dirty ¡

reads"): ¡

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

I

44 ¡

slide-34
SLIDE 34

Anomalies ¡with ¡Interleaved ¡Execu'on ¡

  • Reading ¡uncommi]ed ¡data ¡(WR ¡Conflicts, ¡"dirty ¡

reads"): ¡

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

I

45 ¡

slide-35
SLIDE 35

Anomalies ¡with ¡Interleaved ¡Execu'on ¡

  • Nonrepeatable ¡reads ¡(RW ¡Conflicts): ¡

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

I

46 ¡

slide-36
SLIDE 36

Anomalies ¡with ¡Interleaved ¡Execu'on ¡

  • Nonrepeatable ¡reads ¡(RW ¡Conflicts): ¡

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

I

47 ¡

slide-37
SLIDE 37

Anomalies ¡(Con'nued) ¡

  • Overwri'ng ¡uncommi]ed ¡data ¡(WW ¡conflicts): ¡

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

I

48 ¡

slide-38
SLIDE 38

Anomalies ¡(Con'nued) ¡

  • Overwri'ng ¡uncommi]ed ¡data ¡(WW ¡conflicts): ¡

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

I

49 ¡

slide-39
SLIDE 39

Isola'on ¡Levels ¡

82 ¡

slide-40
SLIDE 40
  • SET ¡TRANSACTION ¡ ¡

ISOLATION ¡LEVEL ¡<level> ¡ ¡ ¡

  • (do ¡aler ¡BEGIN ¡TRANSACTION) ¡

83 ¡

slide-41
SLIDE 41

(Review) ¡Goal: ¡ACID ¡Proper'es ¡

  • ACID ¡transac+ons ¡ ¡are: ¡

– Atomic ¡: ¡Whole ¡transac'on ¡or ¡none ¡is ¡done. ¡ – Consistent ¡: ¡Database ¡constraints ¡preserved. ¡ – Isolated ¡: ¡It ¡appears ¡to ¡the ¡user ¡as ¡if ¡only ¡one ¡ process ¡executes ¡at ¡a ¡'me. ¡ – Durable ¡: ¡Effects ¡of ¡a ¡process ¡survive ¡a ¡crash. ¡

84 ¡

What ¡happens ¡if ¡system ¡crashes ¡between ¡commit ¡ and ¡flushing ¡modified ¡data ¡to ¡disk ¡? ¡

slide-42
SLIDE 42

Problem ¡defini'on ¡

  • Records ¡are ¡on ¡disk ¡
  • for ¡updates, ¡they ¡are ¡copied ¡in ¡memory ¡
  • and ¡flushed ¡back ¡on ¡disk, ¡at ¡the ¡discre+on ¡of ¡

the ¡O.S.! ¡

– (although ¡you ¡can ¡force ¡it) ¡

D ¡

85 ¡

slide-43
SLIDE 43

Problem ¡defini'on ¡

  • Records ¡are ¡on ¡disk ¡
  • for ¡updates, ¡they ¡are ¡copied ¡in ¡memory ¡
  • and ¡flushed ¡back ¡on ¡disk, ¡at ¡the ¡discre+on ¡of ¡

the ¡O.S.! ¡

– (although ¡you ¡can ¡force ¡it) ¡ ¡

  • SoluLon: ¡Write-­‑ahead ¡log ¡

D ¡

86 ¡

slide-44
SLIDE 44

Durability ¡-­‑ ¡Recovering ¡From ¡a ¡Crash ¡

  • At ¡the ¡end ¡– ¡all ¡commi]ed ¡updates ¡and ¡only ¡

those ¡updates ¡are ¡reflected ¡in ¡the ¡database. ¡

– All ¡ac've ¡Xacts ¡at ¡'me ¡of ¡crash ¡are ¡aborted ¡when ¡ system ¡comes ¡back ¡up. ¡

  • Some ¡care ¡must ¡be ¡taken ¡to ¡handle ¡the ¡case ¡
  • f ¡a ¡crash ¡occurring ¡during ¡the ¡recovery ¡

process! ¡

D

98 ¡