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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

863 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

27 Experts available now in Live!

Get 1:1 Help Now