transaction processing book, chapter 6.6. problem: with a single user…. you run a query, you get...

17
Transaction processing Book, chapter 6.6

Upload: julie-griffith

Post on 13-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Transaction processing

Book, chapter 6.6

Page 2: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Problem: With a single user….

you run a query, you get the results, you run the next, etc.

But database life is rarely that simple.

Multi user systems where users can be accessing data at the same time.

Page 3: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Protecting the database

• Recovery – restoring the database to a correct state after a failure

• Concurrency control – allow simultaneous use without unwanted interference

Page 4: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Passenger 1

Opens seat assignment

querySeat 13 C shows as available

Thinks

Takes a phone call

Select 13 Cupdate query

Passenger 2

Opens seat assignment

querySees Seat 13 C as available

Select 13Cupdate query

Who gets the seat?

Page 5: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Transaction

• “a group of operations that need to be performed together”

• Serializability – The property that a transaction with respect to other transactions must appear to be run in serial (one after another)…or in other words, one transaction completes before the next can begin.

Page 6: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

In the prior case

• If Passenger 1 and 2 are both shown the seat, passenger 2 chooses it first, passenger 1 should not be allowed to complete his/her transaction.

• If passenger 1 is told, “I’m sorry, but that seat is now taken” and the choice is blocked, passenger 1 will need to make another choice and more importantly, the database is left in a consistent state.

Page 7: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Options

• We could block passenger 2 from seeing anything until passenger 1 is done (phone call)

• We could display both, but not let P2 make a choice until P1 is done

• We could let P2 make a choice and then alert P1 that their choice failed

• We could let P1’s choice go through and negate P2’s

Page 8: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Atomicity

• A transaction is considered an atomic statement – it is an all or nothing proposition.

• Another example

Page 9: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Banking example

• Customer transfers money

• Check account 1• Deduct from account 1• Credit account 2

• What if the last bit doesn’t go through?

Page 10: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Transaction

• One or more statements that must execute as a group.

• START TRANSACTION begins a group of statements

• COMMIT tells the system to make the transaction permanent (everything is ok)

• ROLLBACK tells the system to go back to the original state (problem)

Page 11: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Properties

• A – Atomic• C – Consistent• I – Isolated• D - Durable

Page 12: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Problems

• Lost update problem – See airline example

• Uncommitted update problem – One transaction “looks at” a change, but the change never goes through because of a problem; it’s result is based on wrong information.

• Inconsistent analysis – Query once , query a second time after data has changed

Page 13: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Problems

• Non repeatable read• 1 reads• 2 changes• 1 reads again and its different

• Phantom transaction• 1 reads• 2 inserts• 1 reads again and now new row

Page 14: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

No problem

• 2 Transactions may read data at the same time

• 2 Transactions may operate on completely different data elements

• 2 Transactions operate on the same data set in serial time

Page 15: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Serial process

Transaction 1

Transaction 2

Transaction 3

Transaction 4

Page 16: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Interleaved process

Transaction 1A

Transaction 2A

Transaction 1B

Transaction 2B

Page 17: Transaction processing Book, chapter 6.6. Problem: With a single user…. you run a query, you get the results, you run the next, etc. But database life

Preventing concurrency issues

• Locking – halt operation of one transaction while the resource required is in use

• Timestamping – transactions are stamped with a start time and checked against the resource change time.