Re: Update Integrity

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 02/16/05


Date: Wed, 16 Feb 2005 21:59:31 +0100

On Wed, 16 Feb 2005 09:36:31 -0500, Steven Lazan wrote:

>Looking for opinions:
>
>What are some of the preferred methods for maintaining data integrity during
>updates. In other words, how to prevent one user from overlaying data that
>another user just saved. If two users are looking at the same data on two
>different PCs. User 1 changes data and saves, then a few seconds later User
>2 changes and saves, User 2 data overlaid User 1.
>
>The thought was to have a col in the table (either number or datetime) that
>increases with each update. Then the user submitting the update passes to
>the proc the last value of that col (called LastUpdate) along with the
>update values. If the value of LastUpdate has changed, then return error to
>the user asking them to refresh their screen since the data has changed
>since they retrieved it.
>
>Are there other methods or suggestions for this circumstance.

Hi Steve,

This is the most common method. It's usually called "optimistic locking"
(optimistic, as it assumes no update conflicts will occur and takes extra
steps to correct in the rare cases that they do).
In SQL Server, you don't even have to manually increase the value of an
integer column, you can use a column with the rowversion (aka timestamp)
datatype/attribute - SQL Server will increase the value of this column
every time a row is updated. If you include it in the select and in the
WHERE clause for the UPDATE, you have imnplemented optimistic locking:
  SELECT @Col1 = Col1, @Col2 = Col2, ..., @RowVers = RowVers
  FROM MyTable
  WHERE KeyColumn = @KeyValue
  do something with the values in @Col1, @Col2, ...
  UPDATE MyTable
  SET Col1 = @Col1, Col2 = @Col2, ...
  WHERE KeyColumn = @KeyValue
  AND RowVers = @RowVers
  IF @@ROWCOUNT <> 0
  BEGIN
    -- Handling for row changed by other connection
  END

You also asked for other methods; there is one called "pessimistic
locking" (pessimistic, as it assumes that update conflicts will occur and
therefore takes steps to prevent them).
You can implement pessimistic locking in two ways:
(a) Start a transaction when the "change values" screen is opened, take an
exclusive lock on the data (use UPDLOCK hint on the select), keep the
transaction open until the user ends the screen, either disbanding or
saving his changes, or
(b) Use a column to indicate that a user has reserved the row for change
and check the value of that column before allowing another user to start
changing the values
In both options, a second user doesn't get a chanace to overwrite the
changes made by the first, since he simply is not allowed to start making
changes until the first user is ready.
Pessimistic locking isn't used much, mainly because neither option is very
scalable; option (a) has serious impact on concurrency as well and option
(b) is prone to locks staying around in the database forever if a
connection is broken ungracefully.

Best, Hugo

-- 
(Remove _NO_ and _SPAM_ to get my e-mail address)


Relevant Pages

  • Re: Best Multi-user strategy for ADO on an Access (mdb) file
    ... >edits etc. ... Well neither optimistic or Pessimistic locking is going to let the users know ... Optimistic locking when they try to post the data. ...
    (borland.public.delphi.database.ado)
  • Re: Best Multi-user strategy for ADO on an Access (mdb) file
    ... Optimistic locking really gives you a lot more flexibility than ... the user edits, or how requery and reapply the edits as you suggest. ... pessimistic locking gives other problems, ... If there is an optimistic locking multi-user strategy that can cover the ...
    (borland.public.delphi.database.ado)
  • Re: How I dramatically sped up DB file updating
    ... With that you mean to say that pessimistic locking would definietly be ... I myself did not find such references. ... optimistic locking is that it can be much faster and makes more ...
    (comp.lang.pascal.delphi.misc)
  • Re: Multiuser Problem
    ... Pessimistic locking - in todays modern languages this is not really the ... requires that you have a database transaction locking the row of the record ... Allow the user to edit the row but before you write the data back to ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Datensatz sperren
    ... bei der ein einziger Benutzer ganz alleine auf seine lokal gespeicherte ... optimistischen Locking besser bedient. ... Macht man das selbe mit optimistischen Locking, dann ist ein Datensatz ... Hier ist in der Tat optimistic Locking der bessere Weg. ...
    (microsoft.public.de.german.entwickler.dotnet.datenbank)