Question

When using DataReaders, and in this example Response.Write (but I guess it would apply to any Controls), does slow clients affect number of connections? The average connection pool usually has about max 15 connections. So if 100 users hit the application and 50 of them have very slow connections with high latencies, will the number of connections exceed the maximum number of connections?

If using a DataTable, I'd imagine that all the data is loaded into memory so the connection isn't left open while doing the Response.Write.

Example:

using(SqlDataReader reader = DataUtility.GetReader("select * from employees"))
{
   while(reader.Read()) 
   {
      Response.Write(reader["emp_id"]).ToString();
      Response.Write(reader["username").ToString();
   }
}

vs

DataTable emps = DataUtility.GetDataTable("select * from employees");
foreach(DataRow row in emps.Rows)
{
      Response.Write(row["emp_id"]).ToString();
      Response.Write(row["username").ToString();
}
Was it helpful?

Solution

The response is buffered on the server, so the code won't wait for the response to actually be sent to the client. A slow connection doesn't make the server code run slower.

Still, your server code will be more efficient if you first fetch all the data that you need from the database, then close the database connection and use the data to create the response. That way you can have the database connection open as short a time as possible, using less database resources.


Side note: I see that you use select * in your code, which fetches all fields from the table. You should specify only the fields that you actually will use, so that you fetch as little data as possible from the database.

OTHER TIPS

We can't answer this question with the code given. The number of connections depends on when you open and close the connection in your code. If you wait for all the data to be sent to client before you close your connection then a slow connection would have an effect.

Most C# code is not written this way. Typically you open a connnection, do some work and close the connection. Then do additional processing and send the response to the client.

Show us more code, I expect you are not closing you connections and this is causing your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top