Pessimistic concurrency SQL Server 2005

Posted on 2007-08-08
Last Modified: 2013-11-07
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.

Question by:jnsimex
    LVL 75

    Expert Comment

    by:Aneesh Retnakaran
    >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

    Author Comment

    > 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.

    LVL 5

    Accepted Solution

    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.

    Author Comment

    thanks, LOCKID worked great.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now