It is a .Net application which works with an external device. When some entity (corresponds to the row in a table) wants to communicate with device, the corresponding row in the SQL Server table should be locked until the device return a result or SQL Server times out.
I need to:
- lock a specific row in a table so that row could be read, but could not be deleted or updated.
- locking mechanism should run in a separate thread so that application's main thread works as usual
- lock should be released if a response has made
- lock should be released after a while if no response is received
What is the best practice?
Is there any standardize way to accomplish this?
Should I:
run a new thread (task) in my C# code and begin a serializable transaction, select desired row transactionally and wait to either time is up or cancellation token is received?
or use some combination of
sp_getapplock
and ...etc?