Link to home
Start Free TrialLog in
Avatar of jimr111998
jimr111998

asked on

MS SQL Row Locking and Release

I am using MS SQL 2005.
I want to
- obtain the next unlocked record in a table and lock it all at once so no other user can fetch it.
- work on the record for maybe 5 minutes. ( while doing this no one else can use the record)
- update the record and release the lock on it.

Also if the user  decides not to update, a way to release the lock on
this record.

Your help is greatly appreciated.
Thank you
Avatar of Kobe_Lenjou
Kobe_Lenjou
Flag of Belgium image

Have you checked the HOLDLOCK and READPAST table hints?

The HOLDLOCK will hold your lock on the record until the currect transaction end.
You have to define you own transaction with BEGIN TRAN and, depending in the user a COMMIT TRAN or ROLLBACK TRAN

http://msdn.microsoft.com/en-us/library/ms187373.aspx
Avatar of Anthony Perkins
Create a column in the table that indicates the row is locked.  Set it when you are editing it and clear it when you are done.  In addition, have a job run every 5 minutes to clear the flag on rows that show locked more than 5 minutes.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimr111998
jimr111998

ASKER

angelIII:

Very good. I can do that. I don't think I need any more info. This will work fine.
Thanks for the ideas.
Very good response!