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.
If you don't want to touch database then you can handle locks on application level too.
Create new class for locks.
Create static property for your application (let's say, dictionary).
Create methods LockObject and UnlockObject for you application.
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.
Don't forget to mark solution providing post as "Answered".
PSP_152890
Participant
784 Points
155 Posts
How to solve concurrent site issue
Aug 24, 2010 09:39 AM|LINK
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.
ASP.net 2.0: C# : Concurrent issue
Saravana Prakash P.
ignatandrei
All-Star
137690 Points
22151 Posts
Moderator
MVP
Re: How to solve concurrent site issue
Aug 25, 2010 04:49 AM|LINK
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
PSP_152890
Participant
784 Points
155 Posts
Re: How to solve concurrent site issue
Aug 26, 2010 06:11 AM|LINK
Thanks Ignatandrei.
Can we able to handle this scenario in c# code itself.
Thanks in Advance,
Saravana Prakash P.
Saravana Prakash P.
ignatandrei
All-Star
137690 Points
22151 Posts
Moderator
MVP
Re: How to solve concurrent site issue
Aug 26, 2010 08:59 AM|LINK
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)
atconway
All-Star
16846 Points
2756 Posts
Re: How to solve concurrent site issue
Aug 28, 2010 12:05 PM|LINK
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
DigiMortal
Contributor
5728 Points
954 Posts
MVP
Re: How to solve concurrent site issue
Aug 28, 2010 05:34 PM|LINK
If you don't want to touch database then you can handle locks on application level too.
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.
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
DigiMortal
Contributor
5728 Points
954 Posts
MVP
Re: How to solve concurrent site issue
Aug 28, 2010 07:56 PM|LINK
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.
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
mbanavige
All-Star
135170 Points
15505 Posts
ASPInsiders
Moderator
MVP
Re: How to solve concurrent site issue
Aug 28, 2010 08:22 PM|LINK
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.