views:

66

answers:

3

I have the scenario like this,

My environment is .Net2.0, VS 2008, Web Application

I need to lock a record when two members are trying to access at the same time.

We can do it in two ways,

  1. By Front end (putting the sessionID and record unique number in the dictionary and keeping it as a static or application variable), we will release when the response is go out of that page, client is not connected, after the post button is clicked and session is out.

  2. By backend (record locking in the DB itself - need to study - my team member is looking ).

Is there any others to ways to do and do I need to look at other ways in each and every steps?

Am I missing any conditions?

+6  A: 

You do not lock records for clients, because locking a record for anything more than a few milliseconds is just about the most damaging thing one can do in a database. You should use instead Optimistic Concurrency: you detect if the record was changed since the last read and re-attempt the transaction (eg you re-display the screen to the user). How that is actually implemented, will depend on what DB technology you use (ADO.Net, DataSets, Linq, EF etc).

If the business domain requires lock-like behavior, those are always implemented as reservation logic in the database: when a record is displayed, it is 'reserved' so that no other users can attempt to make the same transaction. The reservation completes or times out or is canceled. But a 'reservation' is never done using locks, is always an explicit update of state from 'available' to 'reserved', or something similar.

This pattern is also describe din P of EAA: Optimistic Offline Lock.

Remus Rusanu
A: 

If your talking about only reading data from a record from SQL server database, you don't need to do anything!!! SQL server will do everything about managing multi access to records. but if you want to manipulate data, you have to use Transactions.

Jalal Amini
A: 

I agree with Ramus. But still if u need it. Create a column with name like IsInUse as bit type and set it true if one is accessing. Since other guys will also need same data at same time then u need to save your app from crash .. so at every place from where the data is retrieved you have to put a check if IsInUse is False or not.

Amit Ranjan