20

Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Embed Size (px)

DESCRIPTION

The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes and writes to the database User B makes changes and writes to the database Updates entered by user A are lost Second editor may have write access only when the first editor has done. Uncommitted Dependency (Dirty Read) User A opens a document and makes changes User A saves the record while still working on it User B opens the same record and prints out 100 copies User A decides to not change the document and rolls back to the original state User B has dirty data that should never be distributed Second editor may only read the data once we know that the changes by the first editor are final.

Citation preview

Page 1: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes
Page 2: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Module Road Map

Page 3: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

The Scope of the Problem A range of potential problems

Lost Updates

User A reads a record User B reads the same record User A makes changes and writes to the database User B makes changes and writes to the database Updates entered by user A are lost  Second editor may have write access only when the first editor has done.

  Uncommitted Dependency (Dirty Read) 

User A opens a document and makes changes User A saves the record while still working on it User B opens the same record and prints out 100 copies User A decides to not change the document and rolls back to the original state User B has dirty data that should never be distributed

  Second editor may only read the data once we know that the changes by the first editor

are final.

Page 4: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

The Scope of the Problem Nonrepeatable Read 

User A is talking to a customer interested in product X The customer decides to go ahead with the purchase User B now modifies the specification for product X User A proceeds to the final stage of the transaction but sees that the

specification has changed There is no way of going back to the original read of the data  We need a mechanism for locking data during the span of a transaction.

  Phantom Reads 

User A is preparing a mail shot to customers User A runs a query to generate the list of customers User B edits a record for one customer, deletes a record for another and adds

a new mail shot customer User A is now working with data that has either changed or doesn’t exist

(Phantom data)  We need a mechanism for locking batches of data to stop data being modified.

Page 5: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Disconnected Architecture

Page 6: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Optimistic and Pessimistic LockingPessimistic Locking

We assume a high chance of two records being modified simultaneously. When one user opens the record it is locked and another user may not access it until the first user is done.

Not suitable for disconnected architecture

Optimistic Locking 

We assume the chances of two people accessing the same record are unlikely. When we write back the data we check to see if the record was modified by another process / user.

Doesn’t actually solve the problem

Page 7: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Optimistic LockingRead the data plus the current time stamp in

the recordChange the dataBefore we write back the changes see if the

time stamp we recorded has changed in the original data

If it is the same then write the data

Page 8: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Three Approaches

1. Use the data adapter2. Use a time stamp3. Check each field

Page 9: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Using Data Adapters for Optimistic Locking

So far used read only data tables…

Using parameters to write data back…

Page 10: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Use Data Adapters to do the WorkclsAlternateDataConduitDataAdapter Concurrency.zipNot using stored procedures

Page 11: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Execute Method

SQL not Stored ProceduresNo loop for parametersUses OleDbCommandBuilder

Page 12: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Query Results Collection

Get + Set allowing read write access

Page 13: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Write to Database Method

Tells the data adapter to update the database with any changes

Page 14: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

ADO.NET Locking

Modify the record in Access and then press F5

Page 15: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

DBConcurrencyException

Page 16: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Catching the Error

Page 17: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Using Time Stamps for Optimistic LockingBasis of this weeks lab workModify the table

Page 18: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Read a record read the stamp

Page 19: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Use Stored Procedure Code

Page 20: Module Road Map The Scope of the Problem A range of potential problems Lost Updates User A reads a record User B reads the same record User A makes changes

Checking Each Field

Could design our stored procedure so that it checks on a per field basis