Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have one application which is using MS SQL 2008 database.

In the database I have one long storeprocedure which is doing many operation like reading some of the tables and check some specific record exists or not, if not exits then it create the record other wise update it, these type of operation happens on different 3-4 tables and many other checking going on.

And this storeprocedure called very frequently around (within 1 second called 10 times) from different SQL connection from UI. Now we have one table "X" in which need to check particular record exist then need to update it other wise need to insert it.

But now issue is because of this storeprocedure called very fast, so table "X" got two records (one need to insert and one need to update that inserted record) at the same time. so instead of insert one record only it making two entry in the table "X"

So can you please guide me how I should use different locking which can resolve my issue.

Thanks,
Posted
Updated 20-Mar-14 22:01pm
v3

1 solution

Hello,

I have found the solution of the issue which I was facing.

By Adding the sp_getapplock statement at first statement of sp and sp_releaseapplock at last statement of sp it resolve my issue.

I've added it like below :

SQL
CREATE PROCEDURE [dbo].[TestSP]
AS
BEGIN

    BEGIN TRANSACTION;
    EXEC sp_getapplock @Resource = 'TestSPLock', @LockMode = 'Exclusive';

    //Logic of SP

    EXEC sp_releaseapplock @Resource = 'TestSPLock';
    COMMIT TRANSACTION;
END



Thanks,
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900