Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I doing something wrong from a connection pooling point of view?

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.

like image 218
Matt W Avatar asked Dec 30 '25 07:12

Matt W


1 Answers

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:

  • ADO will pool connections that have exactly the same connection string
  • Most ado.net objects implement IDisposable and should be disposed of either explicitly or in a using block. This includes connections, commands, datasets and data readers
  • Linq 2 SQL and EF data contexts also implement IDisposable and should be disposed
like image 78
TGnat Avatar answered Jan 01 '26 21:01

TGnat