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

  • SEARCH
  • Home
  • 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 7537847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:57:39+00:00 2026-05-30T06:57:39+00:00

I am getting this error on my website: Timeout expired. The timeout period elapsed

  • 0

I am getting this error on my website:

Timeout expired. The timeout period elapsed prior to obtaining a
connection from the pool. This may have occurred because all pooled
connections were in use and max pool size was reached.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Timeout expired.
The timeout period elapsed prior to obtaining a connection from the
pool. This may have occurred because all pooled connections were in
use and max pool size was reached.

This is the C# class that is being reused everywhere (I think the leak is here):

public class Generator
{

SqlConnection cn = null; // new SqlConnection(connectionString);

    public SqlConnection Connection {
        get {
            if (cn == null) {
            cn = new SqlConnection("Server=xxxxx,1433;Database=xxxxx;User ID=xxxxx;Password=xxxxx;Trusted_Connection=False;Encrypt=True;");
            }
            if(cn.State != ConnectionState.Open)
                cn.Open();

            return cn;
        }
    }

}

Then in my methods I use it like this:

        var cmd = _generator.Connection.CreateCommand();
        cmd.CommandText = "SELECT * FROM SomeTable";
        var reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            //DO SOMETHING
        }
        reader.Close();

EDIT: In the class of my methods I attempt to share the same SQL connection across multiple methods like this:

private Generator _generator;

public HomeController()
{
    InitializeConnection();
}

private void InitializeConnection()
{
    _generator = new Generator();
}

Can anyone see how I can fix the leak or what is causing my max pool issue?

  • 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-30T06:57:41+00:00Added an answer on May 30, 2026 at 6:57 am

    You need using blocks to make sure the objects are disposed.

    using (var cmd = _generator.Connection.CreateCommand()) {
        cmd.CommandText = "SELECT * FROM SomeTable";
        using (var reader = cmd.ExecuteReader()) {
    
                while (reader.Read())
                {
                    //DO SOMETHING
                }
        }
    }
    

    I also wouldn’t use a Connection property. It’s better to open and close connections as needed and allow connection pooling to do its job.

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

Sidebar

Related Questions

Copied a working website from one server to another, now I'm getting this error
Anyone getting this error when using the new free chart controls MS bought from
I have this large (and oddly formatted txt file) from the USDA's website. It
has anyone used JiBX before? JiBX website I keep getting this error when attempting
I'm getting this error Could not load type 'System.Web.UI.ScriptReferenceBase' from assembly 'System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
I searched throughout the website for the error I am getting this error while
I'm getting this Javascript error on my website ( http://beta.radonsystems.net ): TypeError: Result of
I have been getting this error for quite some time now and Google has
I receive error emails from my website whenever an exception occurs. I am getting
Getting this error when try to compile a .net 1.1 website: Visual Studio .NET

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.