Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
40 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 600 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 70

Accepted Solution

by:
Scott Pletcher earned 1400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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 extract information from SQL Server on Database, Connection and Server properties

636 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