22
Transaction Models 2. Transactions 3 Transaction Properties Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–1

Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models

2. Transactions

3 Transaction Properties

4 Problems in Multi-User Mode

5 Commands for Transaction Control

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–1

Page 2: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models

2. Transactions

3 Transaction Properties

4 Problems in Multi-User Mode

5 Commands for Transaction Control

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–1

Page 3: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models

2. Transactions

3 Transaction Properties

4 Problems in Multi-User Mode

5 Commands for Transaction Control

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–1

Page 4: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models

Transactions in Multi-User ModeRun-time integrity

Avoid errors resulting from simultaneous access of multiple usersto the same dataMultiple programs running simultaneously→ concurrent and competing processesTransaction as a processing unit

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–2

Page 5: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models

Example scenarios

Almost simultaneous seat reservations for flights from many travelagencies→ Place could be sold several times when several travel agenciesidentify the space as availableOverlapping operations of a bank accountStatistical database operations→ Results are falsified, if data is changed during the calculation

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–3

Page 6: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Transaction Properties

Transaction

A transaction is a sequence of operations (actions), which transfers adatabase from a consistent state into another eventually changedconsistent state, applying the ACID properties.

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–4

Page 7: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Transaction Properties

ACID Properties

Atomicity:Transactions are either completed, or not performed at all.Consistency:If the database is in a consistent state before a transaction starts,the database is also consistent after the transaction has ended.Isolation:A user who is working on the database should not notice anyother user working on it.Durability:The result of a transaction must be permanently stored within thedatabase, after the transaction is completed.

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–5

Page 8: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Transaction Properties

Commands of a transaction language

Beginning of a transaction: Begin-of-Transaction-Command BOT(in SQL implicitly)commit: The transaction should be completed successfullyabort: The transaction must be aborted

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–6

Page 9: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Problems in multi-user mode

Non-repeatable readRead of inconsistent statesDirty readPhantom ProblemLost updateMulti-user anomalyProblems with cursor references

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–7

Page 10: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Non-repeatable Read: Example I

Constraint X = A + B + C at the end of the transaction T1

X and Y are local variablesTi is the transaction iIntegrity constraint: A + B + C = 0

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–8

Page 11: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Non-repeatable Read: Example II

T1 T2

X := A;Y := A/2;A := Y ;C := C + Y ;commit;

X := X + B;X := X + C;commit;

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–9

Page 12: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Read of Inconsistent States: ExampleIntegrity constraint X + Y = 0

T1 T2

read(X );read(Y );X := X − 1;write(X );Y := Y + 1;

read(X );read(Y );

write(Y );commit;

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–10

Page 13: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Dirty Read: Example

T1 T2

read(X );X := X + 100;write(X );

read(X );Y := Y + X ;write(Y );commit;

abort;

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–11

Page 14: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Lost Update: Example

T1 T2 Xread(X ); 10

read(X ); 10X := X + 1; 10

X := X + 1; 10write(X ); 11

write(X ); 11

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–12

Page 15: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

The Phantom Problem: Example

T1 T2

select count (*)into Xfrom employeewhere dept=’CS’;

insertinto employeevalues (6789, ’Lilo’, ’Pause’, ’CS’);commit;

update employeeset Bonus =Bonus+10000/Xwhere dept=’CS’;commit;

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–13

Page 16: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Multi-user anomaly: Example I

Integrity constraint A = B

T1 := 〈A := A + 10;B := B + 10〉T2 := 〈A := A · 1.1;B := B · 1.1〉

Each T1 and T2 keeps the IC isolated

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–14

Page 17: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Multi-user anomaly: Example II

T1 T2 A Bread(A); 10 10A := A + 10;write(A); 20

read(A);A := A · 1.1;write(A); 22read(B);B := B · 1.1;write(B); 11

read(B);B := B + 10;write(B); 22 21

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–15

Page 18: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Problems with cursor references: Example

T1 T2

Position cursor C1on next tuplewith property P(Tuple A)

Changeproperty P → P ′

of ARead current tuple

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–16

Page 19: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Problems in Multi-User Mode

Problems with cursor: SQL-notation

T1 T2

declare C1 cursor forselect *from Productwhere Price < 100;...

update Productset Price = 100where ProdNr = 42;

fetch C1 into ...

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–17

Page 20: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Commands for Transaction Control

Operations: user commands

User commands that may influence the aborting of a transaction:I commit attempts to perform a commit not always successful

(integrity violation) commit is only guaranteed if DBMS confirmsthe commit

I abort forces final abort of a transactionI Other database operations that may lead to an abort: division by

zero, integrity violations, ...

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–18

Page 21: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Commands for Transaction Control

Operations: Internal

commit performs a final commit internallyTwo versions for abort:

I abort+restart aborts the transaction but also attempts tosuccessfully complete the transaction by performing a restart (byintervention of scheduler; if abort+restart fails repeatedly, finalabort is possible as well)

I abort+stop performs the final abort (due to explicit user requestor due to irreparable integrity violations)

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–19

Page 22: Transaction Models 2. Transactions - dbse.ovgu.de Processing/_/tp_2.pdfTransaction Models 2. Transactions 3 Transaction Properties 4 Problems in Multi-User Mode 5 Commands for Transaction

Transaction Models Commands for Transaction Control

Operations: Relation

abort + restart abort + stop

abort

commit

DB operationscommit

user

leve

lin

tern

al

Thomas Leich, Gunter Saake Transaction Management Last updated: 17.10.2018 2–20