简体   繁体   English

进行连续ajax调用的正确方法是什么?

[英]What is the right way of making continuous ajax calls?

I have some code like this to update a graph on my page in real-time every 30 seconds: 我有这样的代码可以每30秒实时更新页面上的图形:

var counter = 30;

$(function() {
    prepare();
    update();
});

function update() {
    $("#timer").html("Refreshing in " + counter + " seconds...");
    counter--;

    if (counter == 0) {
        counter = 30;
        prepare();
    }

    setTimeout(update, 1000);
}

function prepare() {
    $.ajax({
        type: "POST",
        url: "Service.asmx/GetPlotData",
        contentType: "application/json; charset=utf-8",
        success: OnSuccess, // this function plots the new data
        error: OnError
    });
}

This seems to be working fine except after 16-20 hours of continuously making ajax calls, I get an error back from the server: 这似乎工作正常,除非在连续进行16-20小时的ajax调用后,服务器发回错误:

Timeout expired. 超时时间已到。 The timeout period elapsed prior to obtaining a connection from the pool. 从池中获取连接之前已经过超时时间。 This may have occurred because all pooled connections were in use and max pool size was reached. 这可能是因为所有池化连接都在使用中,并且达到了最大池大小。

I fired up the debug console and this is what I observe: 我启动了调试控制台,这是我观察到的:

AJAX calls are getting fired correctly AJAX呼叫正确触发

在此处输入图片说明

Before the 16-20 hour period, there are some instances where the latency increase (this is where the Timeout error is seen for the first time) 在16-20小时之前,有些情况下延迟增加(这是第一次看到Timeout错误的情况)

在此处输入图片说明

Finally, the code manages to hit some bottleneck. 最后,代码设法解决了一些瓶颈。 Latency increases for every single call and the front-end breaks. 每次呼叫和前端中断的延迟都会增加。 No call after the blue arrow below returns any data. 下面的蓝色箭头返回任何数据后,没有呼叫。 Instead, it throws the timeout error. 相反,它将引发超时错误。

在此处输入图片说明

I am sure I am doing something fundamentally wrong. 我确定我做的事情根本上是错的。 Any ideas on how to address this problem? 关于如何解决此问题的任何想法?

EDIT: Server-side code 编辑:服务器端代码

My connection string: 我的连接字符串:

Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true

The code to pull the records: 拉记录的代码:

try
{
    string ConString = Constants.connString;
    con = new SqlConnection(ConString);

    cmd = new SqlCommand(sql, con);
    con.Open();
    dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        // Add the records into an object
    }

}
catch (Exception x)
{
     // Send back some error text.
     // This is what is giving out the Timeout error
}
finally
{
    con.Close();
}

Unless I am missing something, I am closing the connection after getting the records using the con.Close() or is there anything else I need to do? 除非我丢失了某些东西,否则在使用con.Close()获得记录后,我将关闭连接,或者还有其他需要做的事情吗?

EDIT 2: Changing the above code as follows. 编辑2:如下更改上面的代码。 Is this correct? 它是否正确?

try
{
    string ConString = Constants.connString;

    using (con = new SqlConnection(ConString))
    {
        cmd = new SqlCommand(sql, con);
        con.Open();
        dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            // Add rows to object
        }
    }

}
catch (Exception x)
{
    // Handle error
}
finally
{
    con.Close();
}

It looks like a server-side issue with too many connections to a database. 看起来是服务器端问题,与数据库的连接过多。 How're you connecting to the DB? 您如何连接到数据库? Are you closing the connection after using it? 使用后是否要关闭连接? Try closing the connection after a number of connections. 尝试在多个连接后关闭连接。

"I get an error back from the server" makes me think this is a server side resource leak. “我从服务器返回错误”使我认为这是服务器端资源泄漏。 What happens if you run two browser tabs in parallel, or two browsers in parallel, or two hosts with their own browsers hitting the server in parallel? 如果您并行运行两个浏览器选项卡,或并行运行两个浏览器,或两个具有各自浏览器的主机并行运行服务器,会发生什么情况?

Does your browser memused rise over time? 您的浏览器是否随着时间推移而上升?

If you have access to server side logs, that would also be a point to dive in. 如果您有权访问服务器端日志,那也很容易理解。

EDIT 编辑

After seeing server code, you may want to close the reader as well for safety; 查看服务器代码后,出于安全考虑,您可能还希望关闭阅读器。 I would be surprised if this caused a leak, but you never know. 如果这引起泄漏,我会感到惊讶,但您永远不会知道。 I'm more familiar with Java, where this can cause a leak depending on the underlying driver being used. 我对Java更熟悉,在Java中这可能会导致泄漏,具体取决于所使用的基础驱动程序。

dr.Close();
con.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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