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
38 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Assisted Solution

by:Branislav Borojevic
Branislav Borojevic earned 150 total points
ID: 41734621
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
ID: 41734653
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:
Scott Pletcher earned 350 total points
ID: 41734850
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
ID: 41738316
Thank you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 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