• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

Pessimistic concurrency SQL Server 2005

I have a windows application that 5 users will use to create and update records on a SQL Server 2005. When a user is updating a record, I need to have the record lock so nobody can access it. If another user trys to access the locked record, I want a popup to appear telling them the record is locked and is currently being updated.

It sounds like I want pessimistic concurrency lock on the database but I am not sure how to implement this......If anybody can show me some examples or point me in the right direction, that would be great.

  • 2
1 Solution
Aneesh RetnakaranDatabase AdministratorCommented:
>When a user is updating a record, I need to have the record lock so nobody can access it
Do u mean to diallow the "SELECT operations also "  ?  
If you put those update statement inside a trasnsaction, then the other user wont be able to update that row untill that transaction completes.

also another option to prevent these sorts of updates is to modify that table to add one "TimeStamp" column. The timestamp value will automatically updated whenever you tries to modify that particular row (INSERT /UPDAtE ). and implement the logic something like this
1. fetch the timestamp value of that particular row
2. UPDATE that row with ur condition + timestamped value = the above stored one
jnsimexAuthor Commented:
> Do u mean to diallow the "SELECT operations also "  ?  

I think they should be able to select the record but are prevented from making modifications until the original user exits the form. Let me give you an example:

This applicaton has a summary form which shows the order header information. When a user selects an order, the order header/detail form pop ups. At this point, if another user selects the same order from the order summary, they should be able to select and view it but all the controls on the order header/detail page will be disabled because it currrently opened by another user. Once the original user has exit from the order header/detail form, then that order will be available for editing by the other users.

You can use the row wise record locking for this.
When ever a record is selected, it will be tagged with the (@@SPID + Datetime) LOCKID whihc will be unique at any instance of the sql server.

When a user try to select a record,
1. check if the LOCKID field is null
2. iF = NULL, then place the new LOCKID of the current user in that field.
3. Do the updatioin and on commit, set this LOCKID field back to NULL.
4. If LOCKID field is NOT NULL, check whther IP address is still valid.
5. If its not a valid IP address, then lock the record and select it.
jnsimexAuthor Commented:
thanks, LOCKID worked great.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now