Results 1 to 29 of 29

Thread: Opinions - with MS SQL Server - how do you lock?

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Question Opinions - with MS SQL Server - how do you lock?

    dee-u PM'd me last night and asked about locking in MS SQL Server when doing an action query like - UPDATE ... WHERE...

    I'm looking for opinions and techniques.

    How do you all LOCK rows or recordsets? Both INLINE SQL and STORED PROCEDURES.

    Thanks in advance.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Opinions - with MS SQL Server - how do you lock?

    I guess I've never really thought about it too much. Our user base isn't large enough at any one location for this to be a huge problem. If there is going to be a conflict in something, we use transactions to "lock" the tables.

    Or are we talking about user A pulls up a record, and user B tries to pull up that same record? If that's the case. We don't lock it. Last save gets their data updated. And with the way our system works and the type of system that it is, that hasn't been a problem. And this is something our clients know right up front (it's covered in the demo.)

    I'm not even sure how we would handle something like that. At least not with out some re-engineering.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Opinions - with MS SQL Server - how do you lock?

    I usually let SQL Server decide which is the best locking method (row...page...table).

    Row level locking is the "best" method for maximum concurrency, but it eats memory like ... something huge and hungry. And then there is table level locking, which does the exact opposite of row level locking.

    I have come to the conclusion that in most cases SQL Server does a great job in deciding locking levels.

  4. #4

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Opinions - with MS SQL Server - how do you lock?

    Here in our shop we basically do the same thing - let SQL decide how to lock.

    I've always had huge fears about holding locks on a row by some client PC that the user goes to lunch - causing deadlock issues. I'm not sure how true those fears are.

    We have one process - which adjudicates medical claims - that takes about 10 or so minutes to run. It's one STORED PROCEDURE - called by one of the users from the VB client program. An ADO STORED PROCEDURE call is an implicit transaction (I believe we have BEGIN TRAN/COMMIT in the SPROC anyway...)...

    At any rate, this SPROC does process around 2000+ rows in the medical claim table - with SQL doing it's automatic locking, we do sometimes get hourglass waits on the other user PC's. This is expected - so not a lot we can do about it. There are other SPROCS that run in the several minute area - we've recently put some WITH (NOLOCK) hints in these SPROCS to help avoid locks when they run.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by techgnome
    Or are we talking about user A pulls up a record, and user B tries to pull up that same record? If that's the case. We don't lock it. Last save gets their data updated. And with the way our system works and the type of system that it is, that hasn't been a problem. And this is something our clients know right up front (it's covered in the demo.)
    We basically do this as well - last user to update gets the data put into the row.

    Are other people doing something besides this?

    Still curious...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    Re: Opinions - with MS SQL Server - how do you lock?

    We let SQL Server handle our locking here as well, with one exception.

    I do have a procedure where I lock a table that contains a "next" barcode number. We have multiple workstations accessing/updating the table, so in order to prevent then grabbing the same number, I lock the table, grab the value, then set the next number and release the lock. I don't have to worry about deadlocking here because the user doesn't control the lock, the procedure does.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  7. #7

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by vb_dba
    We let SQL Server handle our locking here as well, with one exception.

    I do have a procedure where I lock a table that contains a "next" barcode number. We have multiple workstations accessing/updating the table, so in order to prevent then grabbing the same number, I lock the table, grab the value, then set the next number and release the lock. I don't have to worry about deadlocking here because the user doesn't control the lock, the procedure does.
    Just curious - to clarify...

    You mean in the STORED PROCEDURE you lock the table - grab the row - update the row - unlock the table - leave the STORED PROCEDURE?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by szlamany
    ... we do sometimes get hourglass waits on the other user PC's. This is expected - so not a lot we can do about it. There are other SPROCS that run in the several minute area - we've recently put some WITH (NOLOCK) hints in these SPROCS to help avoid locks when they run.
    Couldn't you do BEGIN and COMMIT TRAN for each claim, unless of course the business logic requires that everything to be committed in the same transaction?

    The NOLOCK (equivalent to READUNCOMMITTED) option you use sounds a little bit scary to me, as it can cause users (or the other SPROCS) to make decissions on dirty data.

  9. #9
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by vb_dba
    We let SQL Server handle our locking here as well, with one exception.

    I do have a procedure where I lock a table that contains a "next" barcode number. We have multiple workstations accessing/updating the table, so in order to prevent then grabbing the same number, I lock the table, grab the value, then set the next number and release the lock. I don't have to worry about deadlocking here because the user doesn't control the lock, the procedure does.
    This is a good example where you should specify the locking hint yourself. I'm guessing you use TABLOCKX or a XLOCK hint.

  10. #10

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by kaffenils
    Couldn't you do BEGIN and COMMIT TRAN for each claim, unless of course the business logic requires that everything to be committed in the same transaction?

    The NOLOCK (equivalent to READUNCOMMITTED) option you use sounds a little bit scary to me, as it can cause users (or the other SPROCS) to make decissions on dirty data.
    This large SPROC returns a big-old recordset at the end that gets turned into the "Adjudication Register Report".

    We have a question that the user answers before the run - "Do you want to commit?". They basically can get a "pre-check register" to look for errors. Also, if an adjudication error is encountered during the run, it automatically rollback's the transaction - but still returns the recordset (an abbreviated one - with just the "hard" error rows that need correction).

    So yes - it's a business requirement that the entire run be in one transaction. I dislike long running transactions - but unfortunately this one has to stay

    The NOLOCK hint was used sparingly - with threads posted here prior to it's implementation. The SPROC that uses it is ok with getting stale or uncommitted data - since it's determining eligibility for claims and will run every day anyway - so stale data by a day is not a big problem.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  11. #11

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by kaffenils
    This is a good example where you should specify the locking hint yourself. I'm guessing you use TABLOCKX or a XLOCK hint.
    We avoid locking for situations like this by using a WHERE clause that only updates if the row is not changed by another user - here's an example SPROC (not a real one - just whipped it up)...

    Code:
    Create Procedure GetValue 
    As
    Declare @LastVal int
    Declare @NextVal int
    
    Do_Again:
    
    Set @LastVal=(Select LastValue From SomeTable Where ConfItem='LastTrans')
      -- Get the last value from the config table
    
    Set @NextVal=@LastVal+1
      -- Increment it by one
    
    Update SomeTable Set LastValue=@NextVal
       Where ConfItem='LastTrans' and LastValue=@LastVal
      -- Update the row - but only if it's not changed by another user
      -- Shouldn't really be possible anyway since this SPROC is a "transaction"
    
    If @@RowCount=0 Goto Do_Again
      -- If the ROWCOUNT=0 - meaning we did not update - must be because
      -- we need to do it again...
    
    Select @NextVal "NextValue"
      -- Return the value

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by techgnome
    I guess I've never really thought about it too much. Our user base isn't large enough at any one location for this to be a huge problem. If there is going to be a conflict in something, we use transactions to "lock" the tables.

    Or are we talking about user A pulls up a record, and user B tries to pull up that same record? If that's the case. We don't lock it. Last save gets their data updated. And with the way our system works and the type of system that it is, that hasn't been a problem. And this is something our clients know right up front (it's covered in the demo.)

    I'm not even sure how we would handle something like that. At least not with out some re-engineering.

    Tg
    Ditto, ditto and ditto. This is the exact same scenerio as employeed in my shop.

  13. #13
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by szlamany
    Just curious - to clarify...

    You mean in the STORED PROCEDURE you lock the table - grab the row - update the row - unlock the table - leave the STORED PROCEDURE?
    I first update the row (contrary to my original post), placing a Table lock on the table while the update takes place. Once I've updated the table, I commit the transaction and select the value from the table.

    Quote Originally Posted by kaffenils
    This is a good example where you should specify the locking hint yourself. I'm guessing you use TABLOCKX or a XLOCK hint.
    Update ... With (TABLOCK, HOLDLOCK) is what I use.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  14. #14
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    Re: Opinions - with MS SQL Server - how do you lock?

    I have worked at a shop though where we actually did row level locking. They were C applications that connected to an Informix database. It used client-side cursors to lock the row so no one else could even view the record while it was being looked at. It worked very well, but I don't see a need for that where I'm at now.
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  15. #15
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by szlamany
    Code:
    Create Procedure GetValue 
    As
    Declare @LastVal int
    Declare @NextVal int
    
    Do_Again:
    
    Set @LastVal=(Select LastValue From SomeTable Where ConfItem='LastTrans')
      -- Get the last value from the config table
    
    Set @NextVal=@LastVal+1
      -- Increment it by one
    
    Update SomeTable Set LastValue=@NextVal
       Where ConfItem='LastTrans' and LastValue=@LastVal
      -- Update the row - but only if it's not changed by another user
      -- Shouldn't really be possible anyway since this SPROC is a "transaction"
    
    If @@RowCount=0 Goto Do_Again
      -- If the ROWCOUNT=0 - meaning we did not update - must be because
      -- we need to do it again...
    
    Select @NextVal "NextValue"
      -- Return the value
    Thats also a way of doing it. Never though of that approach. Clever

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Opinions - with MS SQL Server - how do you lock?

    I also keep locking as brief as possible, and let the server handle the lock type. For my systems there are relatively few users per app, so there aren't often noticeable issues at all (I think 3 in 5 years).


    One difference from the methods mentioned so far, is that I let the first update through.

    Any subsequent updates are temporarily rejected (basically returning a pre-defined fail code instead of the "Do_again" loop), and the user gets the option to overwrite the values that have been altered. The altered values are shown on a highlighted version of the edit screen, or a grid for larger data sets.

    (admittedly in a couple of older systems, and a couple with large data sets, the last update always goes through)

  17. #17

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Opinions - with MS SQL Server - how do you lock?

    SI - what is the method you use to determine that the row was changed by another user, so that the subsequent update fails out?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Opinions - with MS SQL Server - how do you lock?

    I've used various methods over the years, I started out using the methods provided in DAO/ADO recordsets (which give a specific error when you update).

    I've since moved on to the following methods (depending on the DBMS & database structure):

    a) if there are fields for "last update time", just compare that for the updated rows;
    b) temp tables on the server to store the "old" data, which can be compared just before the update (obviously this can cause issues with larger data sets);
    c) "old" data stored in the app, and returned in where clauses (only for single rows, and kept to a minumum due to the network load issues)

  19. #19

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Opinions - with MS SQL Server - how do you lock?

    When we migrated our data from the mainframes into MS SQL Server, we added a "TDATE" column to the end of each and every table. In all our SPROCS that do INSERT/UPDATE to tables, we set the TDATE=GETDATE()...

    Our main goal of this is to track "reality" when a user calls in a bogus problem (oh - I changed that last week, how come it's just doing this now! or... I never touched that person!)...

    I've always imagined that we could also use this field as SI suggests - make sure that an UPDATE includes in the WHERE clause a check to see that the TDATE field hasn't changed...

    But we have yet to see a real need for that.

    I hope we get some more opinions - I know that there are quite a few more SQL people our there!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  20. #20
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by szlamany
    There are other SPROCS that run in the several minute area - we've recently put some WITH (NOLOCK) hints in these SPROCS to help avoid locks when they run.
    I just came to think of, and you probably know this allready, that SQL Server 2005 (whenever it is released) can solve this "problem" by using row versioning, also called snapshot isolation. There is of course a performance cost since SQL Server has to keep the last committed rows in the TempDB until the current transaction is committed or rolled back.

  21. #21
    Fanatic Member
    Join Date
    Sep 2004
    Location
    Jakarta, Indonesia
    Posts
    818

    Re: Opinions - with MS SQL Server - how do you lock?

    i'm know it's not relevant to this thread..but this link might help u to check for what process and the LOCK they hold
    http://www.sommarskog.se/sqlutil/aba_lockinfo.html

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL,
    Kill Database Processes

  22. #22
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by erickwidya
    i'm know it's not relevant to this thread..but this link might help u to check for what process and the LOCK they hold
    http://www.sommarskog.se/sqlutil/aba_lockinfo.html
    Great work !! I will definately use it. Until now I have used EM to find out who is locking what and who is blocking who. REP POINTS for this one.

  23. #23
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Opinions - with MS SQL Server - how do you lock?

    I wish to barge now... What will happen with this scenario?

    VB Code:
    1. 'User1: 05:06:01
    2. adoConn.Execute "UPDATE Table1 SET Field1=A WHERE Field2=Y"
    3.  
    4. 'User1: 05:06:02
    5. adoConn.Execute "UPDATE Table1 SET Field1=B WHERE Field2=Y"

    And this?

    VB Code:
    1. 'User1: 05:06:01
    2. adoConn.Execute "UPDATE Table1 SET Field1=A WHERE Field2=Y"
    3.  
    4. 'User1: 05:06:01
    5. adoConn.Execute "UPDATE Table1 SET Field1=B WHERE Field2=Y"
    Last edited by dee-u; Aug 3rd, 2005 at 11:15 PM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  24. #24
    Addicted Member
    Join Date
    Jun 2005
    Posts
    139

    Re: Opinions - with MS SQL Server - how do you lock?

    In the 1st case, the second update will return '0 rows updated'
    and
    in the 2nd case both will do the update, the order will depend on which process aquires the lock first.

  25. #25
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by pranoy
    In the 1st case, the second update will return '0 rows updated'
    How do you figure? It should still get one row updated....


    In our shop, in both cases, who ever got there last would get the update. I assume it's a User 1, User 2, where User 1 sets field to "A" while User 2 is setting the same data to "B".... first, our system is such that it is extremely unlikely to be happening (because if it is, then that means that two customer service people are on the phone at the same time to the same (client's) customer) but in the event it does, in both cases, odds are User 2 gets the update.

    Tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  26. #26

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Opinions - with MS SQL Server - how do you lock?

    Dee-u - did you mean for your example to have a FIELD1 in the UPDATE/SET and a FIELD2 in the WHERE clause?

    We discussed earlier in the thread using a WHERE clause trick that included the UPDATE/SET field in the WHERE clause so only one user would update.

    Otherwise, I agree with TG - both UPDATE's are going to run in the example you posted - regardless of the times they fire. If they fire at exactly the same millisecond, they will still both run.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  27. #27
    Addicted Member
    Join Date
    Jun 2005
    Posts
    139

    Re: Opinions - with MS SQL Server - how do you lock?

    Sorry..TG u r right. I dont know what I was thinking when I wrote that.

  28. #28
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Opinions - with MS SQL Server - how do you lock?

    I've edited my sample, what would be the value of Field1 with each scenario I've given? Specially at the same millisecond, will it differentiate them through nanoseconds? And what if they are also executed at the same nanosecond?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  29. #29

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Opinions - with MS SQL Server - how do you lock?

    Quote Originally Posted by dee-u
    I've edited my sample, what would be the value of Field1 with each scenario I've given? Specially at the same millisecond, will it differentiate them through nanoseconds? And what if they are also executed at the same nanosecond?
    With a SQL database there is nothing being executed at the same exact time. Everything is sequentially processed - every request for ACTION is put into the LOG file for processing. If the LOG file has two requests to update the same row in it - one will be first, and one will be second - that is the nature of the sequential processing of the LOG file.

    If the LOG file indicates that the row the second UPDATE is attempting to touch is locked, the the second ACTION request in the LOG file will hold until the first request processes OR is will deadlock and timeout.

    There is no such thing as two requests occuring at the same time. Remember that a single program called SQLSERVER.EXE is taking each ADO request for ACTION on the DATABASE and processing them sequentially.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width