Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6870945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:44:44+00:00 2026-05-27T03:44:44+00:00

I have a simple DB manager class (a grander name than it’s abilities deserve):

  • 0

I have a simple DB manager class (a grander name than it’s abilities deserve):

class DbManager
{
    private MySqlConnectionStringBuilder _connectionString;

    public DbManager()
    {
        _connectionString = new MySqlConnectionStringBuilder();
        _connectionString.UserID = Properties.Database.Default.Username;
        _connectionString.Password = Properties.Database.Default.Password;
        _connectionString.Server = Properties.Database.Default.Server;
        _connectionString.Database = Properties.Database.Default.Schema;
        _connectionString.MaximumPoolSize = 5;
    }


    public MySqlConnection GetConnection()
    {
        MySqlConnection con = new MySqlConnection(_connectionString.GetConnectionString(true));
        con.Open();
        return con;
    }

}

I then have another class elsewhere that represents records in one of the tables, and I populate it like this:

class Contact
{
    private void Populate(object contactID)
    {
        using (OleDbConnection con = DbManager.GetConnection())
        {
            string q = "SELECT FirstName, LastName FROM Contacts WHERE ContactID = ?";

            using (OleDbCommand cmd = new OleDbCommand(q, con))
            {
                cmd.Parameters.AddWithValue("?", contactID);

                using (OleDbDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        reader.Read();
                        this.FirstName = reader.GetString(0);
                        this.LastName = reader.GetString(1);
                        this.Address = new Address();
                        this.Address.Populate(ContactID)
                    }
                }
            }
        }
    }
}


class Address
{
    private void Populate(object contactID)
    {
        using (OleDbConnection con = DbManager.GetConnection())
        {
            string q = "SELECT Address1 FROM Addresses WHERE ContactID = ?";

            using (OleDbCommand cmd = new OleDbCommand(q, con))
            {
                cmd.Parameters.AddWithValue("?", contactID);

                using (OleDbDataReader reader = cmd.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        reader.Read();
                        this.Address1 = reader.GetString(0);
                    }
                }
            }
        }
    }
}

Now I thought that all the using statements would ensure that connections are returned to the pool as they’re done with, ready for the next use, but I have a loop that creates hundreds of these Contacts and populates them, and it seems the connections are not being freed up.

The connection, the command and the reader are all in their own using statements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:44:44+00:00Added an answer on May 27, 2026 at 3:44 am

    If the app is multithreaded then you are potentially going to have say 10 threads running at the same time. Each of those needs its own connection but if you are limiting that pool size to 5 then you the 6th thread is going to be unable to get a connection from the pool.

    You may be limiting your threads in some way but I’d suggest increasing the size of your app pool significantly to ensure that you have more connections available than you might have threads. As an indicator the default size (which is normally good enough for most people) is 100.

    Additionally if you have any recursion inside your using block, (eg calling the populate again) as you have indicated you have in comments as opposed to the code above then you are going to run into further problems.

    If you call populate inside the using block then you will have the connection from the parent open and in use (so not reusable) and then the child call will open another connection. If this happens just a few times you will run out of your allocation of connections.

    To prevent this you want to move the secondary Populate call out of the using block. The easiest way is rather than looping through your recordset calling populate for each ID is to add the IDs to a list and then after you’ve closed your connection then do the populate for all the new IDs.

    Alternatively you could just lazily evaluate things like the Address. Stored the addressID in a private field and then make Address a Property that checks if its backing field (not the addressID) is populated and if not then looks it up with the AddressID. This has the advantage that if you never look at the address you don’t even do the database call. Depending on use of the data this may save you a lot of database hits but if you definitely use all the details then it just shifts them around, potentially spreading them out a bit more which might help with performance or maybe just making no difference at all. 🙂

    In general with database access I try to just grab all the data out and close the connection as soon as I can, preferably before doign any complicated calculations on the data. Another good reason for this is that depending on your database query, etc. you could potentially be holding locks on the tables that you are accessing with your queries that could cause locking issues on the database side of things.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: C/C++ line number Hi, I have a simple error manager class that
I'm looking at a simple class I have to manage critical sections and locks,
I have simple form. <form target=_blank action=somescript.php method=Post id=simpleForm> <input type=hidden name=url value=http://...> <input
Need help... I have 3 classes, Manager which holds 2 pointers. One to class
I often have this kind of situation - there's a simple plain class, say,
I have a singleton class called Manager that holds a list of object instances:
I have a simple class which does reverse geocoding, but it causes the app
So I have a class that is basically a manager for 20+ copies of
I have a class that is essentially a message manager (a singleton) that has
I have a simple managed C++ assembly which I am providing unmanaged wrappers for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.