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
33 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
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DTS Connection Failed 7 70
Querying data from 3 SQL tables 2 32
Update a text value in another table 10 40
optimize stored procedure 6 28
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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

778 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