What is the right way of making continuous ajax calls?

Tomehzg

New Member
I have some code like this to update a graph on my page in real-time every 30 seconds:\[code\]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 });}\[/code\]This seems to be working fine except after 16-20 hours of continuously making ajax calls, I get an error back from the server:\[quote\] 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.\[/quote\]I fired up the debug console and this is what I observe:AJAX calls are getting fired correctly
wM3ki.png
Before the 16-20 hour period, there are some instances where the latency increase (this is where the \[code\]Timeout\[/code\] error is seen for the first time)
XjFv1.png
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.
J7Kxj.png
I am sure I am doing something fundamentally wrong. Any ideas on how to address this problem?EDIT: Server-side codeMy connection string:\[code\]Data Source={0};Initial Catalog={1};Integrated Security=True;MultipleActiveResultSets=true\[/code\]The code to pull the records:\[code\]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();}\[/code\]Unless I am missing something, I am closing the connection after getting the records using the \[code\]con.Close()\[/code\] or is there anything else I need to do?EDIT 2: Changing the above code as follows. Is this correct?\[code\]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();}\[/code\]
 
Back
Top