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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:40:44+00:00 2026-05-17T02:40:44+00:00

I was trying to explain to someone why database connections implement IDisposable, when I

  • 0

I was trying to explain to someone why database connections implement IDisposable, when I realized I don’t really know what “opening a connection” actually mean.
So my question is – What does c# practically do when it opens a connection?

Thank you.

  • 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-17T02:40:44+00:00Added an answer on May 17, 2026 at 2:40 am

    There are actually two classes involved in implementing a connection (actually more, but I’m simplifying).

    One of these is the IDbConnection implementation (SQLConnection, NpgsqlConnection, OracleConnection, etc.) that you use in your code. The other is a “real” connection object that is internal to the assembly, and not visible to your code. We’ll call this “RealConnection” for now, though its actual name differs with different implementations (e.g. in Npgsql, which is the case where I’m most familiar with the implementation, the class is called NpgsqlConnector).

    When you create your IDbConnection, it does not have a RealConnection. Any attempt to do something with the database will fail. When you Open() it then the following happens:

    1. If pooling is enabled, and there is a RealConnection in the pool, deque it and make it the RealConnection for the IDbConnection.
    2. If pooling is enabled, and the total number of RealConnection objects in existence is larger than the maximum size, throw an exception.
    3. Otherwise create a new RealConnection. Initialise it, which will involve opening some sort of network connection (e.g. TCP/IP) or file handle (for something like Access), go through the database’s protocol for hand-shaking (varies with database type) and authorise the connection. This then becomes the RealConnection for the IDbConnection.

    Operations carried out on the IDbConnection are turned into operations the RealConnection does on its network connection (or whatever). The results are turned into objects implementing IDataReader and so on so as to give a consistent interface for your programming.

    If a IDataReader was created with CommandBehavior.CloseConnection, then that datareader obtains “ownership” of the RealConnection.

    When you call Close() then one of the following happens:

    1. If pooling, and if the pool isn’t full, then the object is put in the queue for use with later operations.
    2. Otherwise the RealConnection will carry out any protocol-defined procedures for ending the connection (signalling to the database that the connection is going to shut down) and closes the network connection etc. The object can then fall out of scope and become available for garbage collection.

    The exception would be if the CommandBehavior.CloseConnection case happened, in which case it’s Close() or Dispose() being called on the IDataReader that triggers this.

    If you call Dispose() then the same thing happens as per Close(). The difference is that Dispose() is considered as “clean-up” and can work with using, while Close() might be used in the middle of lifetime, and followed by a later Open().

    Because of the use of the RealConnection object and the fact that they are pooled, opening and closing connections changes from being something relatively heavy to relatively light. Hence rather than it being important to keep connections open for a long time to avoid the overhead of opening them, it becomes important to keep them open for as short a time as possible, since the RealConnection deals with the overhead for you, and the more rapidly you use them, the more efficiently the pooled connections get shared between uses.

    Note also, that it’s okay to Dispose() an IDbConnection that you have already called Close() on (it’s a rule that it should always be safe to call Dispose(), whatever the state, indeed even if it was already called). Hence if you were manually calling Close() it would still be good to have the connection in a using block, to catch cases where exceptions happen before the call to Close(). The only exception is where you actually want the connection to stay open; say you were returning an IDataReader created with CommandBehavior.CloseConnection, in which case you don’t dispose the IDbConnection, but do dispose the reader.

    Should you fail to dispose the connection, then the RealConnection will not be returned to the pool for reuse, or go through its shut-down procedure. Either the pool will reach its limit, or the number of underlying connections will increase to the point of damaging performance and blocking more from being created. Eventually the finaliser on RealConnection may be called and lead to this being fixed, but finalisation only reduces the damage and can’t be depended upon. (The IDbConnection doesn’t need a finaliser, as it’s the RealConnection that holds the unmanaged resource and/or needs to do the shut-down).

    It’s also reasonable to assume that there is some other requirement for disposal unique to the implementation of the IDbConnection beyond this, and it should still be disposed of even if analysing the above leads you to believe its not necessary (the exception is when CommandBehavior.CloseConnection passes all disposal burden to the IDataReader, but then it is just as important to dispose that reader).

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

Sidebar

Related Questions

Someone has to be able to explain what I'm doing wrong here! I'm trying
can someone explain what is wrong with this delete command, i am trying to
I am trying to learn assembly language. Can someone explain and/or give an example
Let me explain what I'm trying to do, and if someone could point the
can someone explain why this isn't working? i am trying to put an unsigned
Please can some one explain the javacv method for the pyrDown() method. I'm trying
I am trying to explain to myself the forecasting result from applying an ARIMA
I have another programmer who I'm trying to explain why it is that a
I have legitimate reasons to do what I am trying to explain. I have
First I will explain what I am trying to do. I am trying 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.