I have a asp.net webmethod which is static and it will do delete operation of user’s files in the DB.
The file_name is sent through jQuery as input. But the for DB connection, I need to use a static DBconnection object.
When multiple users delete their own file from UI, the jQuery will hit the same static method, which uses a single Static connection object.
So multiple requests arise at the same time, will this delete only the last(latest request) users file is deleted from the DB? If so how to handle this with jquery-ajax in asp.net.
Please throw some light if i stated something wrong.
[Updated]
Server side code:
public static void DeleteAttachment(int fileId, string fileLoc)
{
SqlDBAccess objDBAccessStat = new SqlDBAccess();
.......
}
Obviously, if you share the DB connection between multiple requests simultaneously there’s not telling what will happen. This is per definition undefined.
What you need to do is that either isolate the thing (make it non-static) or introduce a lock that will force incoming requests to wait indefinitely (until the operation is available).
This isn’t particularly hard per se but requires one to be careful as to not introduce a dead lock.
I recommend though that you don’t make your method static, a non-static method is more isolated and used properly with it’s own connection object won’t be prone to the same concurrency issue.
The important thing here is that the DB connection isn’t a global shared resource when multiple users hit that method or things will go wrong.
Just change it to something like this:
The neat part about the above is that you won’t be spamming the server with connections simply because the connection pool is enabled by default, assuming that you using the
SqlClientdatabase provider. It will however give you a nice isolated scopeconndo your your database work in.