I have a class which uses both ADO.NET and LINQ to access a pair of databases on one server. The tables in the DBs are not extensive and so the entity objects are fairly light. I have written the code as best as I, using experience and net articles of course. For example...
http://dotnetperls.com/sqldataadapter http://www.velocityreviews.com/forums/t71781-set-maximum-pool-size-in-web-config.html http://msdn.microsoft.com/en-us/library/ms971481#adonetbest_topic5
The server which is running my code is only running my code and is not the same server as the db host. From looking at the connections going to the DB server from the my .NET app server (it's a Windows Service, but I don't want to dwell on that as it seem immaterial right now) the number of connections is around 200, but it should certainly be less than that; It should be around 10 as I have set the Max Pool Size to 10 in the appSettings.config.
Could anyone take a look at my connection code and tell me if I'm doing something which would cause the connections to shoot up, please?
Here is my LINQ DB context creation:
private const string ConnectionKey = "SQL2";
protected static string ConnectionString
{
get
{
return _connectionString = (_connectionString == null) ? ConfigurationManager.ConnectionStrings[ConnectionKey].ConnectionString : _connectionString;
}
}
private static string _connectionString = null;
protected static PricingDBDataContext ContextDB
{
get
{
if (_context == null)
{
_context = new PricingDBDataContext(ConnectionString);
}
return _context;
}
}
private static PricingDBDataContext _context = null;
Here is the ADO.NET side of things:
protected DataSet GetDataSet(bool isSproc, string cmdStr, params object[] args)
{
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand(cmdStr, conn))
{
cmd.CommandType = isSproc ? CommandType.StoredProcedure : CommandType.Text;
for (int index = 0; index < args.Length; index += 2)
cmd.Parameters.AddWithValue(args[index].ToString(), args[index + 1]);
conn.Open();
DataSet set = FillSet(cmd);
conn.Close();
return set;
}
}
}
private DataSet FillSet(SqlCommand cmd)
{
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet set = new DataSet();
adapter.Fill(set);
return set;
}
Thanks,
Matt.
As a general rule, if you are doing anything explicit with the connection pool, you are probably doing it wrong. The default settings have served me well since the early days of .Net
Here are some things to keep in mind:
IDisposable and should be disposed of either explicitly or in a using block. This includes connections, commands, datasets and data readersIDisposable and should be disposedIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With