Locking several SQL Server records -


my scenario this. have single entity composed of several rows in several tables. conceptually can seen single document. when user opens "document", associated rows must locked, windows locks file when opened. "document" may open until user chooses close it, don't think transactions viable solution. solution have come have boolean 'locked' field in every table , set true on relevant rows when "document" opened. i'm using sql server 2008 r2.

any ideas?

assuming each document has unique id, portable solution use single table knows every locked document:

create table documentlocked (     doc_id int primary key,     session_id <some type>,     lock_acquired datetime ); 

when want lock document, try insert id of document in question along session id identifying owning session , time locked it. if fails, document locked. session id , lock_acquired columns don't play role in locking. helps display useful information user, like, "this document locked rubio @ 9:43 am."

one problem approach crashed clients can leave documents locked forever, need application-level mechanism clobber locks. lock_acquired column can serve timeout mechanism requiring application update time every often, , using detect stale locks.

if don't care portability, go @demas's answer.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -