Solved

How to RowLock in a Stored Proc?

Posted on 2003-11-21
12
1,834 Views
Last Modified: 2012-08-13
Hi All.

 i have some C# code that basically calls two stored procs.

pseudo code

objThing.Get()  -> this calls stored proc usp_Thing_Get, which is a basic SELECT *

objThing.Money = 100 + intNewAmount;

objThing.Set()  -> this calls stored proc usp_THING_Set, which UPDATES a row.


now, i need to lock the row in the GET, and then UNLOCK IT after i do a set ....

any suggestions please?  i have NO idea how this can be done :(

thanks in advance.
0
Comment
Question by:pure032398
  • 5
  • 2
  • 2
  • +3
12 Comments
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9795788
You should not do it in this way.

Assuming that "pseudo code" is actually pretty much what you want to do, then what you ort to be doing is encapsulating all of this functionality in a single stored procuedre wrapped in a transaction.
0
 
LVL 6

Expert Comment

by:BAlexandrov
ID: 9795797
objThing.BeginTransaction()
...
objThing.Commit()
0
 
LVL 7

Expert Comment

by:wsteegmans
ID: 9796334
Indeed, you could use a Transaction for your problem. When starting a Transaction (BeginTransaction) you can specify the Isolationlevel. So, here you set who can view/change recordfields of a LOCKED record ...

Using Transactions is very simple, ... transactions offer the developer the ability to enforce data integrity by making sure multiple operations can be treated by the engine as an "all or nothing" proposition, thereby never allowing the database to end up in an inconsistent state.

Some code?
I think you have here a nice story:
http://www.c-sharpcorner.com/asp/Code/TransactionsInASPNETDPL.asp

Look also in the MSDN ...

You could also do it on SQL-Server level, with the <table_hint> part in the FROM clause:

Table Hint Syntax

SYNTAX
[ FROM { < table_source > } [ ,...n ] ]

< table_source > ::=
    table_name [ [ AS ] table_alias ] [ WITH ( < table_hint > [ ,...n ] ) ]
   
< table_hint > ::=
    { INDEX ( index_val [ ,...n ] )
        | FASTFIRSTROW
        | HOLDLOCK
        | NOLOCK
        | PAGLOCK
        | READCOMMITTED
        | READPAST
        | READUNCOMMITTED
        | REPEATABLEREAD
        | ROWLOCK
        | SERIALIZABLE
        | TABLOCK
        | TABLOCKX
        | UPDLOCK
        | XLOCK
    }

There are fifteen hints defined in this syntax listing. Thirteen of these hints (HOLDLOCK, NOLOCK, PAGLOCK, READCOMMITTED, READPAST, READUNCOMMITTED, REPEATABLEREAD, ROWLOCK, SERIALIZABLE, TABLOCK, TABLOCKX, UPDLOCK, XLOCK) are considered table-level.
More info: http://www.sql-server-performance.com/rd_table_hints.asp

But, because you call two SPs seperately? I don"t know if this second part will work?
Why not building ONE stored procedure with all the logic and passing parameters ...? It makes things more simple!
0
 
LVL 1

Author Comment

by:pure032398
ID: 9796996
the reason i have two stored procs, is becuase i could do multiple calculations between the GET (select *) and the SET (update ).

i do not wish to pass the calcualtions to the SP becuase under certain curcumstances, it could be very complex - i need to keep the business logic on one Tier, and the Data on another.

the second reason i didn't mention transactions was becuase i do not know much about them -AND- i wasn't sure that if begin a transaction, this ROW LOCKS the data i am after.

why am i so interested in this row locking?

i have a multithreaded game which needs to make sure the data is always consisent.

i know that if i can LOCK a row when i read it (becuase i'm about to change the data), then i'm 100% sure than any other threads that wish to access this row will wait (or timeout) until it's unlocked.

i do not want to have two threads read the same data, and update the same row with different data.  I also do not wish to do some timestamp comparrison on the table / row, becuase then i need to do MORE work to re-read the data, then re-calc, then update ... and i could get could in an infinite loop as many threads are all fighting for the same row, etc.
0
 
LVL 1

Author Comment

by:pure032398
ID: 9797022
a further question relating to a comment above...

if i use transactions, could that row lock between the start and the commit?

eg.

// Somehow knows to lock row #10 (for example)
objThing.BeginTransaction()

// some business logic / calculations here ...
eg. intHealth += 10;   // very simple example, but can be very complex here...

objThing.Commit()
// Row now updated _AND_ unlocked.

??? is this the idea?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9798001
Please maintain these old open questions:

1 07/28/2003 60 How to Serialize two (different) instanc...  Open C#
2 07/20/2003 450 How to maintain Data Integrity with our ...  Open C#

Thanks,
Anthony
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:pure032398
ID: 9800766
Anthony  - those previous posts/open questions are maintained / updated.


now back to the topic ....

can stored_procs manually lock and unlock a row, or does this have to be asked elsewhere?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 9800919
>> those previous posts/open questions are maintained / updated.<<
Thanks, I appreciate it.

Anthony
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 9810826
You can explicitly specify locking using "hints" to increase the locking level from shared to exclusive, which will prevent anyone else from reading the row until you're done *if done as part of transaction*:

BEGIN TRAN

SELECT ...
FROM ... WITH (ROWLOCK, XLOCK)
WHERE ...

UPDATE ...
SET ...

END TRAN


I think, but am not sure, that XLOCK was added in SQL 2K; if so, naturally it won't work if your db is 7.0.
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 9810932
As I and others have said.   Perform a transaction.  If you dont want to make this one stored proc (which is the sensible thing) then at least put a transaction on connection object!
0
 
LVL 1

Author Comment

by:pure032398
ID: 9814201
I figured this out last night .. but yeah, the XLOCK was the answer.

becuase i'm not using one stored proc, because my business logic has to be handled in my C# code, using an ADO.Net Transaction works perfectly.

0
 
LVL 1

Author Comment

by:pure032398
ID: 9814214
One other thing relating to the Accepted Answer -> i found the XLOCK solution becuase i saw some documentation for 'WITH UPDATE" in some Sql doco..

how does WITH UPDATE differ from XLOCK?
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
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.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now