Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform a row lock?

I want to lock one record and then no one may make changes to that record. When I release the lock, then people may change the record.

In the meantime that a record is locked, I want to show the user a warning that the record is locked and that changes are not allowed.

How can I do this?

I've tried all the IsolationLevel levels, but none of them has the behavior I want. Some of the Isolation levels wait until the lock is released and then make a change. I don't want this, because updating is not allowed at the moment a record is locked.

What can I do to lock a record and deny all changes?

I use SQL Server 2008

like image 711
Martijn Avatar asked Sep 22 '10 07:09

Martijn


People also ask

What are row locks?

A rowlock UK: /ˈrɒlək/, sometimes spur (due to the similarity in shape and size), oarlock (USA) or gate, is a brace that attaches an oar to a boat.

How do I lock a row in mysql?

A record lock is a lock on an index record. For example, SELECT c1 FROM t WHERE c1 = 10 FOR UPDATE; prevents any other transaction from inserting, updating, or deleting rows where the value of t. c1 is 10 . Record locks always lock index records, even if a table is defined with no indexes.


2 Answers

With the assumption that this is MS SQL server, you probably want UPDLOCK, possibly combined with ROWLOCK (Table hints). I'm having trouble finding a decent article which describes the theory, but here is quick example:

SELECT id From mytable WITH (ROWLOCK, UPDLOCK) WHERE id = 1 

This statement will place an update lock on the row for the duration of the transaction (so it is important to be aware of when the transaction will end). As update locks are incompatible with exclusive locks (required to update records), this will prevent anyone from updating this record until the transaction has ended.

Note that other processes attempting to modify this record will be blocked until the transaction completes, however will continue with whatever write operation they requested once the transaction has ended (unless they are timed out or killed off as a deadlocked process). If you wish to prevent this then your other processes need to use additional hints in order to either abort if an incompatible lock is detected, or skip the record if it has changed.


Also, You should not use this method to lock records while waiting for user input. If this is your intention then you should add some sort of "being modified" column to your table instead.

The SQL server locking mechanisms are really only suited for use to preserve data integrity / preventing deadlocks - transactions should generally be kept as short as possible and should certainly not be maintained while waiting for user input.

like image 199
Justin Avatar answered Oct 26 '22 04:10

Justin


Sql Server has locking hints, but these are limited to the scope of a query.

If the decision to lock the record is taken in an application, you can use the same mechanisms as optimistic locking and deny any changes to the record from the application.

Use a timestamp or guid as a lock on the record and deny access or changes to the record if the wrong locking key is given. Be careful to unlock records again or you will get orphans

like image 30
stombeur Avatar answered Oct 26 '22 04:10

stombeur