Solved

How to lock down a row in a database table immediately after a particular column has been updated.

Posted on 2016-07-29
4
30 Views
Last Modified: 2016-08-01
I need to somehow lock down a row in a database table immediately afer a particular column in that row has been changed. I need to lock it down in the sense that the row can be read but not updated. The lock down will only need to occur for about 2 seconds. The objective is to prevent two different people from updating the column at nearly the same time. Is there a way to do this in Sql Server?
0
Comment
Question by:brgdotnet
  • 2
4 Comments
 
LVL 4

Assisted Solution

by:Branislav Borojevic
Branislav Borojevic earned 150 total points
Comment Utility
Have you tried setting transaction isolation? That way, no other transactions would be able to modify data that is already being modified by another transaction, but only read it. See below for an example, mabye this helps to get your result:

Try adding the TRANSACTION ISOLATION LEVEL

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

BEGIN TRANSACTION;

    select * from ...

    update ...

COMMIT TRANSACTION;

Open in new window


Serializable is the highest isolation level used.

SERIALIZABLE Specifies the following:

Statements cannot read data that has been modified but not yet committed by other transactions.

No other transactions can modify data that has been read by the current transaction until the current transaction completes.

Other transactions cannot insert new rows with key values that would fall in the range of keys read by any statements in the current transaction until the current transaction completes.

There are, however, other levels of isolation. Check this link for more information: https://msdn.microsoft.com/en-us/library/ms173763.aspx
1
 
LVL 2

Author Comment

by:brgdotnet
Comment Utility
I haven't tried anything sir. I wanted to consult with the experts before attemtping it, and doing it the wrong way. I will look into the suggestions you posted. Thanks.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 350 total points
Comment Utility
Use HOLDLOCK (or SERIALIZABLE) hint to keep lock on that specific table's row(s) only.  Also, since 2 secs is a very long lock in SQL terms, you'll want to explicitly delay the completion of the trans to allow that time to pass.  HOLDLOCK seems more naturally understandable to me, but of course you can use SERIALIZABLE if you prefer:


BEGIN TRANSACTION;
...
UPDATE ... WITH ( HOLDLOCK ) ...
WAITFOR DELAY '00:00:02' --wait two seconds with lock in place

COMMIT TRANSACTION;
0
 
LVL 2

Author Closing Comment

by:brgdotnet
Comment Utility
Thank you.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now