3

I am writing a wrapper for an MSSQL driver and I have been told we need to ensure that every incoming UPDATE or DELETE query contains the ROWLOCK table hint. It would be fantastic if I could do this at a transaction isolation level rather than some whackjob regex of every existing query to include WITH ROWLOCK. Now, I am either SOL or just very confused as to what it is that ROWLOCK actually does, so please let me know if my understanding is flawed.

I believe what I am trying to accomplish is ensure that any rows touched by an UPDATE or DELETE statement will not only remain consistent to the changes caused by my transaction and my transaction alone for its entire duration, but importantly prevent the affected rows from being touched by any other transaction concurrently.

The available isolation levels are (from https://msdn.microsoft.com/en-us/library/ms173763.aspx):

-- Syntax for SQL Server and Azure SQL Database  

SET TRANSACTION ISOLATION LEVEL  
    { READ UNCOMMITTED  
    | READ COMMITTED  
    | REPEATABLE READ  
    | SNAPSHOT  
    | SERIALIZABLE  
    }  
[ ; ] 

It seems as though both READ COMMITED and REPEATABLE READ are useful in preventing the data I am touching from being affected by any external changes in the scope of my transaction. However, if I am correctly interpreting the point of ROWLOCK, I don't just want to preserve the state of the data in my scope, I want to prevent anybody else from touching the data I'm dealing with in any form whatsoever until I am completely wrapped up and my transaction is committed.

I am convinced and hopeful that the problem here is misunderstanding what the point of ROWLOCK is. So let me restate, the task is to include the ROWLOCK table hint in every UPDATE or DELETE statement but do so from a transaction isolation level. I am using the Tedious Node.js driver, though I don't think that is relevant to this question.

7
  • 2
    Can you elaborate on this we need to ensure that every incoming UPDATE or DELETE query contains the ROWLOCK table hint. Nov 18, 2016 at 17:13
  • Maybe saying include the ROWLOCK table hint came from a miscommunication, I believe we are trying to emphasize ROWLOCK over any other type of lock (page, table). The documentation says "Shared locks acquired for READ COMMITTED or REPEATABLE READ are generally row locks, although the row locks can be escalated to page or table locks if a significant number of the rows in a page or table are referenced by the read." I'd like to force them to always be row locks. Nov 18, 2016 at 17:23
  • On top of which, the heavy usage of the word "READ" in these transaction levels is unsettling. I need these rows locked for write operations. Nov 18, 2016 at 17:27
  • i am not sure i understand,you also seems to have some confusion,read this for more info on rowlock:stackoverflow.com/questions/3005973/… Nov 18, 2016 at 17:28
  • Thank you! Reading now. This thread is making me wonder if this is a good idea to begin with, but I will edit the question above to clarify. Nov 18, 2016 at 17:35

1 Answer 1

3

If I understand your question clearly, it looks like you are looking for SNAPSHOT ISOLATION. It does not do row locks but use row versioning. As per your question, it looks like you do not want any locks on "Read" because of performance but wants to maintain the integrity for write operations.

The snapshot isolation level uses row versioning to provide transaction-level read consistency. Read operations acquire no page or row locks but does row versioning with DML statements.

As with (rowlock) is a hint that instructs the database that it should keep locks on a row scope. That means that the database will avoid escalating locks to block or table scope. That will let another query read unrelated rows at the same time instead of having to wait for the delete to complete.

The above mentioned use of rowlock can be achieved using SNAPSHOT ISOLATION where you will be able to read unrelated rows.

4
  • Perfect, I am going to check this out now but it sounds like this could be what I need. Thank you! Nov 18, 2016 at 17:44
  • So then I have a follow up, if I using SNAPSHOT ISOLATION for write operations means no rows get locked, does that mean there is no chance of a deadlock? Nov 18, 2016 at 17:56
  • @PauloBlack, You can read msdn.microsoft.com/en-us/library/tcbchxcb(v=vs.110).aspx This significantly reduces the likelihood of deadlocks for complex queries Nov 18, 2016 at 18:00
  • Perfect, this is exactly what I needed. Thank you again. Nov 18, 2016 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.