0

I have an asp.net page that execute some update to db. The updates takes 2-3 seconds, all these hapends in a method that first read data from db and do some updates. When the page request are less then 2 sec for these action, the db read code, read the same state of records in db, and all update actions of asp.net page requests updates the same records.

How i can prevent these, that the newest request wait untill the oldest finish their execution?

Any ideas?

Harold Sota
  • 7,490
  • 12
  • 58
  • 84

2 Answers2

2

If I'm understanding you correctly, you want to block access to a particular set of methods until the process has been completed.

Easy enough... create a static method and implement a lock(). You can get real fancy with share locks, upgrades to read/write locks, etc... but the basic principal is that you lock your critical sections of shared code so other users cannot enter those sections.

http://msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.71).aspx

The lock keyword marks a statement block as a critical section by obtaining the mutual-exclusion lock for a given object, executing a statement, and then releasing the lock.

static object lockObject = new Object();
static bool inProgress = false;

static void MyProcess()
{
  if(inProgress) return;

  lock(lockObject)
  {
    try{
      inProgress = true;
      // critical code here
    }
    finally
    {
      inProgress = false;
    }
  }
}
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
1

Use the appropriate isolation level and database locks: these would ensure that any client (any transaction to be precise) is correctly handled and data integrity is presereved (check isolation levels).

I'd suggest using READ COMMITTED which ensures that you never read uncommitted data and transactions are queued during updates to the same record.

Strillo
  • 2,952
  • 13
  • 15