locked
How to solve concurrent site issue RRS feed

  • Question

  • User1315112726 posted

    Hi All,


    I have developed a ASP.NET2.0 web site with SQL 2000 backend and number users to site is approx. 5-10.

    I have developed one Administration page which controls user roles to access this site; onclick of Update button in admin page the role gets updated into the system. I have written simple query in the code to update the user roles into database; Ajax implemented in admin page;

    Issue: If 2-3 users tries to update the admin roles; latest update reflected in the database and same displayed in the admin page;

    I am looking for a solution to solve this problem by application code change since i dont have flexible to change the database end; if one users update the system, it should not allow other users to perform any operation; please let me know if any standard approach can be implemented with minor code change.


    Thanks in Advance,

    Saravana Prakash P.

    Tuesday, August 24, 2010 5:39 AM

Answers

  • User197322208 posted

    Of course.


    Let's say user A want to modify role with ID =1 , user B want to modify role with id =1

    Retrieve Role and let's say Role.lastmodified = '2010-01-01 16:01:01'

    Display role to be modified in aspx. Put in 2 hidden textbox the ID 1 and LastModified '2010-01-01 16:01:01'

    So now you have 2 users that have both LastModified '2010-01-01 16:01:01' and ID 1

    On save button the code will be like this :

    try
    {


    Application.Lock(); // ensure non concurent access to role id from database

    Role r = <retrieve  role from database with ID = <id from hidden textbox id>;

    if( r.LastModified > <date from hidden textbox Last modified>)
    {

    //add an validation error to the user to say that the role have been modified by another user

    return; // or throw an error
    }

    r.lastModified =datetime.Now;

    //now obtain values and save Role r to database


    }

    finnally{


    Application.Unlock();

    }


    If user A submits first, then A  role modifications will be saved (r.LastModified > <date from hidden textbox Last modified > is false)

    The , when user B submits, then B role modifications will be not saved (r.LastModified > <date from hidden textbox Last modified > is false)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 26, 2010 4:59 AM

All replies

  • User197322208 posted

    add a column "lastmodified"

    IF , when updating, the LastModified is > then your "lastmodified", then show message to the user that someone else have modified the record

    Wednesday, August 25, 2010 12:49 AM
  • User1315112726 posted

    Thanks Ignatandrei.

    Can we able to handle this scenario in c# code itself.

    Thanks in Advance,

    Saravana Prakash P.

    Thursday, August 26, 2010 2:11 AM
  • User197322208 posted

    Of course.


    Let's say user A want to modify role with ID =1 , user B want to modify role with id =1

    Retrieve Role and let's say Role.lastmodified = '2010-01-01 16:01:01'

    Display role to be modified in aspx. Put in 2 hidden textbox the ID 1 and LastModified '2010-01-01 16:01:01'

    So now you have 2 users that have both LastModified '2010-01-01 16:01:01' and ID 1

    On save button the code will be like this :

    try
    {


    Application.Lock(); // ensure non concurent access to role id from database

    Role r = <retrieve  role from database with ID = <id from hidden textbox id>;

    if( r.LastModified > <date from hidden textbox Last modified>)
    {

    //add an validation error to the user to say that the role have been modified by another user

    return; // or throw an error
    }

    r.lastModified =datetime.Now;

    //now obtain values and save Role r to database


    }

    finnally{


    Application.Unlock();

    }


    If user A submits first, then A  role modifications will be saved (r.LastModified > <date from hidden textbox Last modified > is false)

    The , when user B submits, then B role modifications will be not saved (r.LastModified > <date from hidden textbox Last modified > is false)


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 26, 2010 4:59 AM
  • User-952121411 posted

    I would take a look at the following artcile which highlights how to handle concurrency issues with ADO.NET:

    Handling Data Concurrency Using ADO.NET:

    http://msdn.microsoft.com/en-us/magazine/cc163924.aspx


    Saturday, August 28, 2010 8:05 AM
  • User-525215917 posted

    If you don't want to touch database then you can handle locks on application level too. 

    1. Create new class for locks.
    2. Create static property for your application (let's say, dictionary).
    3. Create methods LockObject and UnlockObject for you application.
    4. Test new functionality.

    Class for locks should look something like this:

    class LockInfo
    {
        public Type ObjectType {get; set;}
        public Int32 ObjectId {get; set;}
        public DateTime Locked {get; set;}
        public string SessionId {get; set;}
    }

    LockObject can be something like this:

    public static bool LockObject(BusinessBase obj)
    {
        var context = HttpContext:Current;
        var session = Context.Session;
        var sessionID = session.ID;
    
        lock(Locks)
        {
            var query = from l in Locks
                             where l.ObjectType == obj.GetType() && l.ObjectId == obj.Id
                             select l;
        
            if(query.Count() > 0)
                return true;
        
            var lockInfo = new LockInfo;
            lockInfo.ObjectType = obj.GeyType();
            lockInfo.ObjectId = obj.Id;
            lockInfo.Locked = DateTime.Now;
            lockInfo.SessionId = SessionId;
    
            Locks.Add(obj.GetHash(), lockInfo);
        }
    
        return true;
    }


    Same way you have to make all other checks and locks modifications. Also you have to take care about expired locks. These may arise when some request unexpectedly ends with uncaught exception. In this case your code that releases locks may not be run and object will be locked until you restart your web server.

    Saturday, August 28, 2010 1:34 PM
  • User-525215917 posted

    I wrote simple lock manager and published it to my blog - Managing business object locks on application level. You can find also Visual Studio 2010 solution there with sample code. 

    Saturday, August 28, 2010 3:56 PM
  • User-158764254 posted

    add a column "lastmodified"
     

    rather than using a datetime column that you need to update yourself, you could also use a timestamp column

    http://msdn.microsoft.com/en-us/library/ms182776(SQL.90).aspx

    sql server will take care of updating the timestamp column each time a record is updated.

     

    Saturday, August 28, 2010 4:22 PM