I have created a web application with asp.net
, roughly 100 users
are using it.
However from time to time people are getting the error message that the connection is still open. Indicating that it was not closed properly.
It appears on random places, not one specific place and no other errors before it.
I know that when I have a bug in the application and it crashes without me gracefully dealing with the error the connection remains open as well and basically everyone will crash because of it. This made me think that everyone uses the same connection object, is it possible that 2 users might have the perfect timing and invoke a function using a DB connection
at the same time causing the error? Is there a way to make sure everyone uses their own connection objects, like put it in their session or something?
I hope you can understand what I mean, I don't think posting any of my code will help since it happens on random places within my project.
They are connection to a SQL Server
using System.Data.SqlClient
.
Find below the function which generates the error. This function is called by the Page_Load, nothing is before it.
public static SqlConnection conn = new SqlConnection("Data Source=Server00\\SQLEXPRESS;Initial Catalog=r2;Integrated Security=true;Connect Timeout=0");
private void populateGameDrop()
{
try
{
conn.Open();
drop_game.Items.Clear();
SqlCommand cmd = conn.CreateCommand();
Access ac = (Access)Session["Access"];
cmd.CommandText = "Select * from dbo.Games where " + ac.GameQuery;
SqlDataReader r = cmd.ExecuteReader();
while (r.Read())
{
drop_game.Items.Add(new ListItem(r["name"].ToString(), r["Abbr"].ToString()));
}
conn.Close();
}
catch (Exception exc)
{
conn.Close();
Log.Error(exc.ToString());
Session["Error"] = exc.ToString();
Response.Redirect("~/YouBrokeIt.aspx");
}
populateServers();
SetSplitScreen();
}