0

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();
   .......
  }
2
  • can you provide the jqueyr script plz Commented Jun 3, 2011 at 9:49
  • The jquery script is working fine.. the problem is with server side. It uses a static method and static DBConnection. Commented Jun 3, 2011 at 9:57

2 Answers 2

3

You can use a Webservice.

$.ajax({
  type: "POST",
  contentType: "application/json; charset=utf-8",
  url: "WebService.asmx/WebMethodName",
  data: "{}",
  dataType: "json"
});

And the WebService method need not be not be static.

[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    [WebMethod]
    public void WebMethodName()
    {
         //Do your stuff here
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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:

static void WebMethod()
{
    using (var conn = ProviderFactory.CreateConnection("connection string goes here"))
    {
        conn.Open();
        // Do work here
    }
}

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 SqlClient database provider. It will however give you a nice isolated scope conn do your your database work in.

3 Comments

Is there a way to use a non-static method as web method and call it from Jquery.. ?
A static method can create instances themselves and call operations on that to. And normally this is what you'd do. I don't even think it's necessary that you have the DB connection as a static member you can create it when you need it. Connection pooling will help out here asuming you're already using SqlConnection?
Cool buddy..this works.. even my code works.. am sorry. the connection string was in a Application variable and that was throwing the compiler err while try to access.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.