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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:26:40+00:00 2026-05-29T05:26:40+00:00

Possible Duplicate: Entity framework 4 not closing connection in sql server 2005 profiler Well,

  • 0

Possible Duplicate:
Entity framework 4 not closing connection in sql server 2005 profiler

Well, lots of developers on stackoverflow are saying that I should not worry to close my connection: my using statement will close the connection for me, here and here and all over the site. Unfortunately, I do not see it happening. Here is my code:

    [Test, Explicit]
    public void ReconnectTest()
    {
        const string connString = "Initial Catalog=MyDb;Data Source=MyServer;Integrated Security=SSPI;Application Name=ReconnectTest;";
        for (int i = 0; i < 2000; i++)
        {
            try
            {
                using (var conn = new SqlConnection(connString))
                {
                    conn.Open();
                    using (var command = conn.CreateCommand())
                    {
                        command.CommandText = "SELECT 1 as a";
                        command.CommandType = CommandType.Text;
                        command.ExecuteNonQuery();
                    }
                    //conn.Close();
// optional breakpoint 1 below
                    }
                }
                catch(SqlException e)
                {
// breakpoint 2 below
                    Console.WriteLine(e);
                }
// breakpoint 3 below
            }
        }

When I enable all breakpoints and start my test, the first iteration succeeds, and I hit breakpoint 3. At this point, the connection is still open: I see it in the Profiler, and sp_who2 outputs it too.

Let’s suppose that at this time I am out for a lunch, and my connection is idle. As such, our production server kills it. To imitate it, I am killing the connection from SSMS.

So, when I hit F5 and run the second iteration, my connection is killed. Unfortunately, it does not reopen automatically, so ExecuteNonQuery throws the following exception: “transport-level error has occurred”. When I run the third iteration, my connection actually opens: I see it as an event in Profiler, and sp_who2 outputs it as well.

Even when I have uncommented my conn.Close() command, the connection still does not close, and when I kill it from SSMS, the next iteration still blows up.

What am I missing? Why can’t using statement close my connection? Why can’t Open() actually open it the first time, but succeeds the next time?

This question has originated from my previous one

  • 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-29T05:26:41+00:00Added an answer on May 29, 2026 at 5:26 am

    When you call SqlConnection.Dispose(), which you do because of the using block, the connection is not closed, per-se. It is released back to the connection pool.

    In order to avoid constantly building/tearing down connections, the connection pool will keep connections open for your application to use. So it makes perfect sense that the connection would still show as being open.

    What’s happening after that, I can’t explain offhand – I know that keeping a random connection open would not cause that, though, because your application can certainly make more than a single concurrent connection.

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

Sidebar

Related Questions

Possible Duplicate: Entity Framework with NOLOCK I'm using EF4 and .Net 4 to load
Possible Duplicate: Entity Framework Code First: decimal precision I'm using Linq-to-Entities with the EntityFramework
Possible Duplicate: Conditional Linq Queries Using Entity Framework 4.0 I have a search condition
Possible Duplicate: What is the best way to compare two entity framework entities? I
Possible Duplicate: Entity Framework Include() strongly typed Ok, I've got an entity framework query
Possible Duplicate: Problem with simpleXML and entity not being defined I have this tag
Possible Duplicate: Entity Framework 4.1 : The navigation property ‘BusinessUser’ declared on type ‘Login’
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: getting identity row value using ADO.NET Entity This is my example code:
Possible Duplicate: Hard-Coded Mock Objects vs Mocking Framework I think I'm finally beginning to

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.