简体   繁体   中英

Internal Server Error - 500 while making simultaneous AJAX requests in ASP.NET MVC4

function InitializeCharts() {
 for (var i = 0; i < arguments.length; i++) {
     compName = arguments[i];
     $.ajax({
         async:true,
         timeout: 60000,
         dataType: "json",
         url: "/SalesDashboard/GetChartData",
         data: { company: compName, from: startDate, to: endDate },
         success: (function () {
             var cmp = compName;
             return function (data) {
                 ChartData[cmp] = data;
                 PrepareChart(data, cmp);
             };
         })(),
         error: function (xhr, ajaxOptions, thrownError) {
             console.log(xhr.status+': '+xhr.statusText);                
             console.log(thrownError);
         }
     });
 }
}

I'm using the above function at Client Side to make AJAX requests and get some data. It's working fine when I'm calling the function like this:

InitializeCharts("ABC"); // A Single parameter

It works fine for a single request but when I'm passing multiple parameters like below, it creates multiple simultaneous requests and gives me " Internal Server Error -500 " Error.

InitializeCharts("ABC", "BBC", "CCC", "DDD", "EEE", "ACD", "CBD", "CDS", "ADB"); // Multiple parameters

It gives me the error for few random companies, sometimes for two, sometimes for three or four. I tried increasing the timeout in AJAX request to 60000 but it didn't work. I also went through IIS Server and increased the connection timeout to 3 minutes but still getting the same response. I know it even doesn't take a single minute to get the data back to the client. If I'm setting the " async " value in ajax request to false, it works but blocks the UI until the data returned for all companies.

Below is my Server Side Code:

static int hitCount = 0;
private string ConnectionString = "server=localhost;database={0};uid=test;password=test;pooling=false;";

private string QueryString_Daily = "SELECT Day(mergefiledate) AS `Date`, COUNT(order_no) AS `Count` FROM orders_new WHERE DATE(mergefiledate)>='{0}' and Date(mergefiledate)<='{1}' GROUP BY DATE(mergefiledate);";

public Task<string> GetChartData(string company, string from, string to)
    {
        hitCount++;
        return Task.Factory.StartNew(() => GetData(company, from, to));
    }

private string GetData(string company, string from, string to)
    {            
         string QuerySTR = string.Empty;
         QuerySTR = string.Format(QueryString_Daily, startDt.ToString("yyyy-MM-dd"), endDt.ToString("yyyy-MM-dd"));
         MySqlConnection mycon = new MySqlConnection(string.Format(ConnectionString, company));
        MySqlCommand cmd = new MySqlCommand(QuerySTR, mycon);
        cmd.CommandTimeout = 0;
        DataTable dt = new DataTable();
        try
        {
            mycon.Open();
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('Database connection error, please try again. ERROR_DETAILS: '" + ex.Message + "');</script>");
            return null;
        }

        if (mycon.State == ConnectionState.Open)
        {

            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            da.Fill(dt);

        }

        if (mycon.State == ConnectionState.Open)
        {
            try
            {
                mycon.Close();
            }
            catch { }
        }

        if (dt.Rows.Count > 0)
        {
    var result = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
             return result;
     }
    else
     {
            return null;
         }

    }

I've been trying to figure out the solution by Googling, digging on stackoverflow.com ... etc but no luck yet. Help is highly appreciated, Thanks a lot in advance.

Thanks a lot everyone for putting efforts. I got the solution. i was in the impression that I've set the command timeout for infinite by using the statement below but actually I was wrong:

cmd.CommandTimeout = 0;

I modified the timeout as :

cmd.CommandTimeout = 600;

and it worked perfectly. Thanks again for every little help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM