C
Chris Ashley
We're getting occasional pooling errors on a web app. It seems to
happen for about 30 seconds or so, and then be fine for several hours,
so it's very difficult to replicate the circumstances in which it
happens. However the app is quite high load (Several hundred users at a
time)...
I use a base connection class which has the following code:
public class DataConnection : IDisposable
{
private SqlConnection sqlConn;
protected SqlCommand sqlCmd;
protected DataConnection()
{
try
{
sqlConn = new SqlConnection(GetDBaseConnectionString());
sqlConn.Open();
sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlConn;
}
catch(System.Data.SqlClient.SqlException eSql)
{
throw new ArgumentException("Error opening database: " +
eSql.Message);
}
}
/* Apparently blank destructor is bad
~DataConnection()
{
}
*/
/// <summary>
/// Our implementation of the Dispose funtion from IDisposable.
/// </summary>
public void Dispose()
{
// Dispose of our db connection, and return it to the pool
sqlConn.Close();
sqlConn.Dispose();
GC.SuppressFinalize(sqlConn);
// Dispose our command object (not as important as the conn, but we
may as well do it here anyway)
sqlCmd.Dispose();
GC.SuppressFinalize(sqlCmd);
/* NOTE: We do NOT dispose ourself here. We have disposed of the db
connection,
which is the most important thing, so we can leave ourselves to be
cleaned up by the GC.
*/
}
private string GetDBaseConnectionString()
{
return ConfigurationSettings.AppSettings["DBConnStr"];
}
}
Then my data retrieval classes inherit the connection class like so:
public class DataQuote : DataConnection
{
public DataQuote() : base()
{
}
public string InitiateNewQuote(int intBranchId)
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "sproc_InitiateNewQuote";
sqlCmd.Parameters.Add("@branchId", intBranchId);
try
{
return sqlCmd.ExecuteScalar().ToString();
}
catch(Exception e)
{
// Fatal error
throw new Exception("Error initiating new quote for branch id " +
Convert.ToString(intBranchId) + ". Error: " + e);
}
}
Is there anything wrong with my implementation?
happen for about 30 seconds or so, and then be fine for several hours,
so it's very difficult to replicate the circumstances in which it
happens. However the app is quite high load (Several hundred users at a
time)...
I use a base connection class which has the following code:
public class DataConnection : IDisposable
{
private SqlConnection sqlConn;
protected SqlCommand sqlCmd;
protected DataConnection()
{
try
{
sqlConn = new SqlConnection(GetDBaseConnectionString());
sqlConn.Open();
sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlConn;
}
catch(System.Data.SqlClient.SqlException eSql)
{
throw new ArgumentException("Error opening database: " +
eSql.Message);
}
}
/* Apparently blank destructor is bad
~DataConnection()
{
}
*/
/// <summary>
/// Our implementation of the Dispose funtion from IDisposable.
/// </summary>
public void Dispose()
{
// Dispose of our db connection, and return it to the pool
sqlConn.Close();
sqlConn.Dispose();
GC.SuppressFinalize(sqlConn);
// Dispose our command object (not as important as the conn, but we
may as well do it here anyway)
sqlCmd.Dispose();
GC.SuppressFinalize(sqlCmd);
/* NOTE: We do NOT dispose ourself here. We have disposed of the db
connection,
which is the most important thing, so we can leave ourselves to be
cleaned up by the GC.
*/
}
private string GetDBaseConnectionString()
{
return ConfigurationSettings.AppSettings["DBConnStr"];
}
}
Then my data retrieval classes inherit the connection class like so:
public class DataQuote : DataConnection
{
public DataQuote() : base()
{
}
public string InitiateNewQuote(int intBranchId)
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.CommandText = "sproc_InitiateNewQuote";
sqlCmd.Parameters.Add("@branchId", intBranchId);
try
{
return sqlCmd.ExecuteScalar().ToString();
}
catch(Exception e)
{
// Fatal error
throw new Exception("Error initiating new quote for branch id " +
Convert.ToString(intBranchId) + ". Error: " + e);
}
}
Is there anything wrong with my implementation?