2012-06-27 26 views
0

在我的MVC应用程序中,我每10秒使用一次重复的ajax调用。有时在数据库服务器与应用程序服务器断开连接之后,应用程序正在运行的浏览器中存在大量内存泄漏。由于重复ajax调用,内存泄漏和DB服务器连接失败

我的代码运行的,

我使用jquery.timer.js重复Ajax调用,每10秒

function class1() { 
} 

class1.prototype. funtion1 = function() { 
    $.ajax({ 
     type: "POST", 
     url: "/TestContoller/ActionOne", 
     success: function (result) { 
      $('#DivActionOne').html(result); 
     }, 
     traditional: true, 
     error: function (req, status, error) { 
     } 
    }); 
} 

//Likewise for funtion2, funtion3, funtion4, funtion5 

var count = 0; 
var timer = $.timer(function() { 
    $('#counter').html(++count); 
    var class1 = new class1(); 
    var funtionOne= class1.funtion1; 
    var funtionTwo= class1.funtion2; 
    var funtionThree= class1.funtion3; 
    var funtionFour= class1.funtion4; 
    var funtionFive= class1.funtion5; 
    var currentSec = count; 
    if ((currentSec % 10) == 0) { 
     funtionOne(); 
     funtionTwo(); 
     funtionThree(); 
     funtionFour(); 
     funtionFive(); 
    } 
}); 
timer.set({ time: 1000, autostart: true }); 

当连接丢失了我检查跟踪日志,发现下面的错误消息:

消息:System.ServiceModel.CommunicationException:底层连接已关闭:预计将保持活动的连接已被服务器关闭。 ---> System.Net.WebException:底层连接已关闭:服务器关闭了预期保持活动状态的连接。 ---> System.IO.IOException:无法从传输连接读取数据:现有连接被远程主机强制关闭。 ---> System.Net.Sockets.SocketException:现有连接被远程主机 强制关闭在System.Net.Sockets.NetworkStream.Read(Byte []缓冲区,Int32偏移量,Int32大小) --- End内部异常堆栈跟踪--- at System.Net.Sockets.NetworkStream.Read(Byte [] buffer,Int32 offset,Int32 size) at System.Net.PooledStream.Read(Byte [] buffer,Int32 offset,Int32大小) 在System.Net.Connection.SyncRead(HttpWebRequest请求,布尔userRetrievedStream,布尔probeRead) ---内部异常堆栈跟踪--- 结束在System.Net.HttpWebRequest.GetResponse() 在System.ServiceModel .Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan超时) --- End of end异常堆栈跟踪---

请帮我整理一下。

+0

更新与跟踪日志的问题。 –

回答

1

因为您正在使用定时器而不是调用先前ajax调用回调函数上的下一个ajax请求。除非你把你的web服务器设置为一个网络花园(允许同时发出请求),否则你需要等到每个服务器完成后才能继续下一个。

把你的ajax调用放到一个函数中,然后调用你的success回调函数,当它完成时再次运行。

当然,你需要设置一些类型的变量来确定它是否完成,因此它不会无限循环。

除此之外的任何东西都需要服务器端优化。

像这样的事情应该工作,当然没有样机,因为我从来没有用过的原型,但在这里它是没有它:

thecount = 0;  
    function class1() { 
     $.ajax({ 
      type: "POST", 
      url: "/TestContoller/ActionOne", 
      success: function (result) { 
       $('#DivActionOne').html(result);      
       thecount++1;      
       if (thecount < 5) { class1(); }      
      }, 
      traditional: true, 
      error: function (req, status, error) { 
      } 
     });    
    } 
+0

感谢您的回复。我试试上述解决方案,并让你知道。我可能会很有用,如果你可以说通常可以做什么样的服务器优化。 –

+0

这可以解决内存泄漏问题。请让我知道为什么应用程序服务器与数据库服务器断开连接? –

+0

对不起,但我不明白你在问什么。服务器优化可以有很多事情,例如优化代码,优化数据库?或者增加更多内存等等。 –