Re: Lock a row

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Louis Davidson (dr_dontspamme_sql_at_hotmail.com)
Date: 01/31/05


Date: Sun, 30 Jan 2005 21:52:50 -0600

I would probably suggest that this is not a great idea. Holding locks from
the client is troublesome, and if the connection dies you are not even going
to see that there was a call in progress. In this case, why not use a
status column (or table, if you want a complete history of status)

Then when the user gets the row, you set the status to in-progress, and set
the user that has the row in use. If they don't complete the call properly,
it will stay in an open state. When the user logs back in, you can check to
see if they have any open sessions and make them deal with them.

All of your other clients will look for rows that are not in-progress or
completed. Use the readpast hint as mentioned by Hugo to keep any clients
from having to wait. This way your management applications can see which
calls are in progress, what their status is, and it will still be "locked"
from the users that it needs to be locked from.

-- 
----------------------------------------------------------------------------
Louis Davidson - drsql@hotmail.com
SQL Server MVP
Compass Technology Management - www.compass.net
Pro SQL Server 2000 Database Design - 
http://www.apress.com/book/bookDisplay.html?bID=266
Note: Please reply to the newsgroups only unless you are interested in 
consulting services.  All other replies may be ignored :)
"souris" <soukkris@viddotron.com> wrote in message 
news:%23Fbdm4oBFHA.2540@TK2MSFTNGP09.phx.gbl...
> Thanks for the information,
> It is what I am looking for.
>
> Souris
>
>
> "Hugo Kornelis" <hugo@pe_NO_rFact.in_SPAM_fo> wrote in message 
> news:4b6ov0dsbu5e3gmhsne0omtr2ul2ac95ae@4ax.com...
>> On Sat, 29 Jan 2005 17:00:05 -0500, souris wrote:
>>
>>>I wanted to locked to hide a record to other user if the record is 
>>>viewing
>>>by one user.
>>>Are there any way to do this?
>>>Does lock the record can hide this record to other user?
>>>
>>>Any information is great appreciated.
>>
>> Hi Souris,
>>
>> You could use the READPAST locking hint. Keep in mind that this is only
>> usefull in some very specific circumstances. If you use this locking hint
>> in a statistical or reporting query, you'll miss some data without having
>> any indication if and how much data you miss.
>>
>> A typical situation where the READPAST hint is useful is a queueing
>> system: each application / connection / agent / whatever reads the first
>> row from a wrok-queue table and acquire an exclusive lock; other agents
>> will read past this row and get the next non-locked one. The exclusive
>> lock ensures that the connection reading the row will be able to change 
>> or
>> even delete is after the data is processed.
>>
>> BEGIN TRANSACTION
>>
>> SELECT TOP 1 .....
>> FROM         WorkQueue WITH (READPAST, XLOCK)
>> ORDER BY     Priority
>>
>> -- Do some stuff
>>
>> IF (...)  -- data changed
>>  UPDATE     WorkQueue
>>  SET        ... = ...
>>  WHERE      .....
>>
>> IF (...)  -- processing completely done
>>  DELETE     WorkQueue
>>  WHERE      .....
>>
>> IF (...)  -- check for unhandled errors
>>  ROLLBACK TRANSACTION
>> ELSE
>>  COMMIT TRANSACTION
>>
>>
>> Best, Hugo
>> -- 
>>
>> (Remove _NO_ and _SPAM_ to get my e-mail address)
>
> 


Relevant Pages

  • Avoiding deadlocks in concurrent programming
    ... This is not really Python specific, but I know Python programmers are ... Since many clients can be making requests at once (>100 per second ... complicated by a backup thread which takes the data every 10 minutes ... The easiest way from a design stand point is have a single lock and let ...
    (comp.lang.python)
  • Re: Client/Server Fujitsu Cobol appplications
    ... Running the clients with shared file access on the server ... Fujitsu Cobol tech. suport says ... LOCK MODE MANUAL WITH LOCK ON MULTIPLE RECORDS ...
    (comp.lang.cobol)
  • Re: out-of-process servers -- how do I force thread separation?
    ... lockbegins a communication session & ensures exclusive access until ... a corresponding unlock() call. ... lock() will return FALSE. ... Clients should be allowed to ...
    (microsoft.public.vc.atl)
  • Re: 2.6.14-rt4: via DRM errors
    ... >> I made a fix to the locking code in main drm a couple of months ago. ... > DRM lock or several, what kind of lock it is or what it's protecting ... > DRM clients to keep from stepping on each other? ...
    (Linux-Kernel)
  • Re: Transaction problem (delete / select)
    ... > I use the isolation level for transactions as read commited, ... Oracle shows the user the table as it was before the lock. ... The readers can specify a readpast hint to skip locked rows, ...
    (microsoft.public.sqlserver.server)