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 579289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:21:04+00:00 2026-05-13T14:21:04+00:00

For years, I’ve experienced very weird problems on all my web applications that connect

  • 0

For years, I’ve experienced very weird problems on all my web applications that connect to a SQL server.

The problem is that if something happens to the database server (server restart or other problem), de web app stops working from that point on, even if the database server is alive and well afterwards.

What happens is that every ADO.NET operation (ExecuteNonQuery, CreateReader, BeginTransaction, …) fails with a InvalidOperationException: “Invalid operation. The connection is closed“. It seems that a call to SqlConnection.Open() retrieves a connection from the application pool which is… closed!

According to the documentation, the connection pool should automatically remove severed connections from the connection pool, but apparantly a closed connection isn’t regarded as “severed”, so the call to SqlConnection.Open() happily returns a closed connection, assuming it is open, without checking this.

My current workaround is to check for the state of the connection right after opening it:

using (SqlConnection connection = new SqlConnection( connectionString ))
{
   connection.Open();

   if (connection.State != ConnectionState.Open)
   {
      SqlConnection.ClearAllPools();

      connection.Open();
   }

   // ...
}

This workaround seems to work for now, but I don’t feel comfortable doing this.

So my questions are:

  1. Why does SqlConnection.Open() return closed connections from the connection pool?
  2. Is my workaround valid?
  3. Is there a better way to handle this?
  • 1 1 Answer
  • 1 View
  • 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-13T14:21:04+00:00Added an answer on May 13, 2026 at 2:21 pm

    I did some similar research into connection pooling a while ago, for a slightly different reason, but hopefully will be of some use. What I found is:

    1. even when you close a connection in code, it is returned to the pool without the connection actually being closed – ready for further use.
    2. if that connection gets severed (i.e. SQL Server restarts), when the connection is returned from the pool for another caller to use and that caller does a .Open on it, it does not error at that point when the database server is still down. This is part of the performance benefit of connection pooling as it’s not actually going back off to the database server to connect.
    3. when you actually try to execute a command against the connection (e.g. ExecuteNonQuery) it is at that point it actually throws an exception

    Connections are automatically removed from the pool, my findings were that this typically occurred within a few minutes after it was last used. So, it may be a timing issue – and they are being cleared up, but not before the connections are attempted to be reused again.

    These were some articles I looked at, at the time:
    Sql Server Google Group
    Using Connection Pooling in ASP.NET

    Edit:
    It does sound odd that the bad connection stays in the pool forever – are you sure it definitely does, and it’s not just multiple bad connections? If you are sure then it sounds like those connections aren’t being released properly within your code. This is another very good article I read a while ago, that says (quote):

    Automatically Flushing Connections

    If a pooled connection remains in the
    “closed but reusable” state for
    between 4 and 8 minutes (an interval
    chosen at random) the connection
    pooling mechanism closes the physical
    connection and discards the pooled
    connection. That is unless the number
    of remaining connections is greater
    than the minimum connections
    configured for the pool (the default
    is 0). Note that a connection must
    have been closed by the application
    (and released back to the pool) before
    it can be subject to automatic
    release. If you don’t close the
    connection in code or orphan the
    Connection object, the pooling
    mechanism will do nothing. No, there
    are no ConnectionString arguments to
    change the timeout value.

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

Sidebar

Ask A Question

Stats

  • Questions 337k
  • Answers 337k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Kevin's Word List Page includes a part of speech database.… May 14, 2026 at 4:05 am
  • Editorial Team
    Editorial Team added an answer I don't think that you can Bind directly to this.… May 14, 2026 at 4:05 am
  • Editorial Team
    Editorial Team added an answer Something like //*[starts-with(@id, 'ceil_')] should do nicely. As for an… May 14, 2026 at 4:05 am

Related Questions

For years I have been using the DEBUG compiler constant in VB.NET to write
For years, maybe 10, I've been fascinated with cryptography. I read a book about
For years and years, I've tried to understand the part of Java specification that
For years I've been using ShellExecute() API to launch the default web browser from
For years Windows XP (32 bit)was my development desktop (personal projects). I develop many

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.