In my MVC application, I am using repeated ajax calls for each 10 secs. Sometimes after some amount of time the DB server gets disconnected from the application server and then there is a huge memory leakage in the browser where the application is running.
My code runs as,
I am using jquery.timer.js for repeated ajax calls for each 10 secs
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 });
When the connection got lost I checked the trace log and found the below error message:
Message: System.ServiceModel.CommunicationException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. —> System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. —> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. —> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
— End of inner exception stack trace —
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
— End of inner exception stack trace —
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
— End of inner exception stack trace —
Please help me in sorting this out.
Because you’re using a timer and not calling the next ajax request on the previous ajax calls callback function. Unless you setup your web server to be treated like a web garden (allow simultaneous requests) then you will need to wait until each one is finished before going to the next one.
Put your ajax call in a function, then call the function in your
successcallback so it runs again when its finished.Of course you would need to set up some type of variable determining if its finished or not so it doesn’t loop infinitely.
Anything beyond that, would require server side optimization.
Something like this should work, without prototype of course, because i’ve never used prototype, but here it is without it: