Closing IDbConnection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Kirk

    Closing IDbConnection

    Hi

    do I need to explicitly close IDbConnection and IDbCommand objects? Or are
    they disposed of when the method they are used in ends?

    Normally I use a construct like:

    using (IDbConnection conn = GetConnection() ) // where "GetConnect ion gets a
    database connection
    {
    // do database stuff
    }

    and as I understand it this ensures that the connection object is correctly
    closed (even if an exception is thrown when the connection object is used).

    But I have also seen code without any form of "using" or
    "try/catch/finally". Will the obtained connection in these cases be
    correctly closed when the method using the connection ends?

    thanks,
    Peter


  • Ciaran O''Donnell

    #2
    RE: Closing IDbConnection

    It is best to use the using(){} statement to ensure the connection is closed
    and releases its resources. I dont think the connection is return to the pool
    just for going out of scope. I think they are released when the are garbage
    collected, which depending on you app might not be to soon after they are
    used.

    HTH

    Ciaran O'Donnell

    "Peter Kirk" wrote:
    Hi
    >
    do I need to explicitly close IDbConnection and IDbCommand objects? Or are
    they disposed of when the method they are used in ends?
    >
    Normally I use a construct like:
    >
    using (IDbConnection conn = GetConnection() ) // where "GetConnect ion gets a
    database connection
    {
    // do database stuff
    }
    >
    and as I understand it this ensures that the connection object is correctly
    closed (even if an exception is thrown when the connection object is used).
    >
    But I have also seen code without any form of "using" or
    "try/catch/finally". Will the obtained connection in these cases be
    correctly closed when the method using the connection ends?
    >
    thanks,
    Peter
    >
    >
    >

    Comment

    • Tom Porterfield

      #3
      Re: Closing IDbConnection

      On Fri, 22 Sep 2006 10:12:17 +0200, Peter Kirk wrote:
      Hi
      >
      do I need to explicitly close IDbConnection and IDbCommand objects? Or are
      they disposed of when the method they are used in ends?
      >
      Normally I use a construct like:
      >
      using (IDbConnection conn = GetConnection() ) // where "GetConnect ion gets a
      database connection
      {
      // do database stuff
      }
      >
      and as I understand it this ensures that the connection object is correctly
      closed (even if an exception is thrown when the connection object is used).
      >
      But I have also seen code without any form of "using" or
      "try/catch/finally". Will the obtained connection in these cases be
      correctly closed when the method using the connection ends?
      The Dispose method on the connection closes the connection, so putting its
      instance in a using block as your example will insure that it is closed.
      If the connection instance is not in a using block as above, and the
      connection is not explicitly closed or disposed, then it will remain open
      until garbage collected. This is not a good practice as it means that
      resource is unavailable to others until such time as the GC does its work.
      --
      Tom Porterfield

      Comment

      • Peter Kirk

        #4
        Re: Closing IDbConnection

        "Peter Kirk" <pk@alpha-solutions.dkskr ev i en meddelelse
        news:ukxgT7h3GH A.3656@TK2MSFTN GP04.phx.gbl...
        do I need to explicitly close IDbConnection and IDbCommand objects? Or are
        they disposed of when the method they are used in ends?
        >
        Normally I use a construct like:
        >
        using (IDbConnection conn = GetConnection() ) // where "GetConnect ion gets
        a database connection
        {
        // do database stuff
        }
        >
        and as I understand it this ensures that the connection object is
        correctly closed (even if an exception is thrown when the connection
        object is used).
        >
        But I have also seen code without any form of "using" or
        "try/catch/finally". Will the obtained connection in these cases be
        correctly closed when the method using the connection ends?
        Thanks for the answers. I had guessed correctly what was happening, but just
        wanted confimation. Now I have been through the code and ensured that all
        connections are disposed correctly (with using constructs). Now the issues
        we were having with the application slowing down markedly every so often
        have disappeared - most like caused by the database running out of
        connections and the garbage collector running to close them.


        Comment

        Working...