Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 1234»»»

Record locking Expand / Collapse
Author
Message
Posted Sunday, December 30, 2012 7:01 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, October 23, 2014 8:48 AM
Points: 29, Visits: 48
Question1: How can a user, say User1, be able to lock a row, say Row1, and any dependent rows, say Row11, Row12, until changes are made to the specific row by User1
Question2: If another user, say User2, wants to access Row1 or Row11, Row12, when these rows are locked by User1, is there a way to let User1 know about it and in the worst case force the unlock, or have some kind of timeout whne User1 is inactive
Thanks,
John
Post #1401247
Posted Sunday, December 30, 2012 10:12 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 3:36 AM
Points: 2,841, Visits: 3,988
YOu are basically asking about LOck mechanism. in your case when DML operation performs then exclusive locks are used by sql engine and it can get eslcalated from row level upto table level (decided by sql server internally) based on the amount of data that query is handling. see http://blogs.msdn.com/b/sqlserverstorageengine/archive/2006/05/17/lock-escalation.aspx

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1401287
Posted Monday, December 31, 2012 1:37 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, October 23, 2014 8:48 AM
Points: 29, Visits: 48
Thanks for your reply and suggestion article. I am doing some reading in the mean time.

One of the problems which I am trying to solve is once a user accesses (checks out) a secific record in a table, I would like all subsequent accesses to this specific record by other users to result in a message saying that the record has ben checked out by UserA and not allow changes if UserA does not release the record.
Post #1401309
Posted Monday, December 31, 2012 1:54 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:39 PM
Points: 40,672, Visits: 37,137
For that you'd need to manually reimplement SQL's locking mechanism, probably with a locking table, manual checks, manual notifications in all code that you have. You then need to consider all the complications of rows getting locked but never unlocked, etc. It's a hell of a lot of work and very easy to get wrong.

iirc there's a chapter on implementing something like this in Expert SQL Server 2005 Development (yes, the 2005 version)



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1401314
Posted Monday, December 31, 2012 2:57 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, October 23, 2014 8:48 AM
Points: 29, Visits: 48
Many thanks for your reply Gail,

I appreciate the fact that it would be easy to mess up. If a user needs to work on a specific record for a few minutes and during this time we want to disallow any modifications to the record can we introduce an extra boolean field in the table which would indicate whether the record is checked out or not?

John
Post #1401329
Posted Monday, December 31, 2012 3:04 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:39 PM
Points: 40,672, Visits: 37,137
john 60195 (12/31/2012)
I appreciate the fact that it would be easy to mess up. If a user needs to work on a specific record for a few minutes and during this time we want to disallow any modifications to the record can we introduce an extra boolean field in the table which would indicate whether the record is checked out or not?


You can.

Every single piece of code that you have must then honour and check that (and there's no way SQL can enforce that, you will have to in your code). You need to take great care that two connections can't both think they locked the record (very easy to do if you haven't got the isolation levels, locks and transactions exactly right in your locking code). You need some process/method to unlock rows that have somehow remained locked even though the user is done (many, many possible causes) while not unlocking rows that the users are just taking their time over.

You're looking at massive amounts of development here, huge testing requirements and very likely a lot of related bugs.

Is it really, absolutely necessary to reimplement the database locking methodology yourself? Is it worth the time and effort?
Grab that book I mentioned and have a read through the chapter on this. It's an entire chapter devoted to the myriad ways of getting this kind of process wrong.



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1401333
Posted Monday, December 31, 2012 3:04 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 3:36 AM
Points: 2,841, Visits: 3,988
john 60195 (12/31/2012)
One of the problems which I am trying to solve is once a user accesses (checks out) a secific record in a table, I would like all subsequent accesses to this specific record by other users to result in a message saying that the record has ben checked out by UserA and not allow changes if UserA does not release the record.
Technically this is possible (as gail commented above ) but not recommended. Can you share the actual requirement(businees requirement) so that we might help you.


-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1401334
Posted Monday, December 31, 2012 5:06 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, October 23, 2014 8:48 AM
Points: 29, Visits: 48
Many thanks for your input to both of you.

I have developed a Dental Practice Management Software package (www.VisualDentist.com) using Access and Jet. While this setup is adequate for a small to medium Dental Practice it is not for a large one, hence the interest to use SQL Server.

The problem which I would like to address deals with accessing the dental record of a patient. This includes information relating to the graphical representation, work details, personal details, financial details etc. Assume the Patient records are in tblPatient (let's forget for the time being any one-to-many relationships with other tables). If a user accesses a record (a patient) in tblPatient, the user will have in front of him a graphical picture of teeth. If the user keeps this picture on his screen for some time (say 5 minutes) and subsequently makes a change, I would like any other users accessing this record during those 5 minutes, to be alerted that the record is under modification and maybe prevented from making any changes until the original user finishes with this patient record.

Does this help?

Regards,
John
Post #1401358
Posted Monday, December 31, 2012 5:35 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 2:39 PM
Points: 40,672, Visits: 37,137
Everything I've said stands. You are looking at a huge amount of development and testing and probably a tonne of bugs and irritations. This isn't a 5 minute job.


Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1401364
Posted Monday, December 31, 2012 6:41 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, October 23, 2014 8:48 AM
Points: 29, Visits: 48
Gail, have taken your advice seriously. I will see whether the functionality can be modified to avoid the headaches.
Thanks,
John
Post #1401377
« Prev Topic | Next Topic »

Add to briefcase 1234»»»

Permissions Expand / Collapse