Conn.Close & Conn.Dispose

Discussion in 'ASP .Net' started by Simon Harris, May 31, 2005.

  1. Simon Harris

    Simon Harris Guest

    Hi All,

    Do I need to use both Conn.Close() & Conn.Dispose() when I have finished
    with an SQL connection?


    --
    I am using the free version of SPAMfighter for private users.
    It has removed 2425 spam emails to date.
    Paying users do not have this message in their emails.
    Try www.SPAMfighter.com for free now!
     
    Simon Harris, May 31, 2005
    #1
    1. Advertising

  2. Simon Harris

    Karl Seguin Guest

    This can lead to heated debate, hopefully I'll answer first and then you can
    ignore everyone else ;)

    Seriously though, this is what Dispose does (actual code):
    switch (this._objectState)
    {
    case ConnectionState.Open:
    {
    this.Close();
    break;
    }
    }
    this._constr = null;


    as you can see, all it does it call Close(). So you might be tempted to say
    "i should just call close and save some stack". However, there is a dipose
    pattern which you should follow. while you can call one, or the other or
    both to achieve the same result, this behaviour could change with future
    release. One day (for example 2.0) Dispose might do more...and then you'd
    have to go through all your code and make sure you had called Dispose.
    Additionally, languages like C# are dispose-aware thanks to the using
    keyword.

    As far as i'm concerned, those two points are enough to make sure I always
    call Dipose on classes which inherit IDisposable.

    As for close, my personal feeling is that it doesn't need to be called if
    you are calling Dispose. And, since it's better to be consistent, I'd say
    never call it. Why? well, none of the .net language are close-aware, and by
    definition of what Dispose does, it would be a mistake from the class
    designer (ie, microsoft) to implement cleanup functionality in close which
    Dispose also woudlnt' do (possibly simply by running close, as it currently
    does).

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Simon Harris" <> wrote in message
    news:...
    > Hi All,
    >
    > Do I need to use both Conn.Close() & Conn.Dispose() when I have finished
    > with an SQL connection?
    >
    >
    > --
    > I am using the free version of SPAMfighter for private users.
    > It has removed 2425 spam emails to date.
    > Paying users do not have this message in their emails.
    > Try www.SPAMfighter.com for free now!
    >
    >
     
    Karl Seguin, May 31, 2005
    #2
    1. Advertising

  3. Simon Harris

    Guest

    If You use Connection.Dispose(),Dispose method itself closes the
    connection and make the reffrence of the connection object to null. Its
    fine if you just use the Connection.Dispose().


    -Ram
     
    , May 31, 2005
    #3
  4. Simon Harris

    Simon Harris Guest

    Thank you both for your answers. I think I'll just use dispose from now on.

    Whilst were on the subject, we had a situation recently where an application
    was not releasing connections back into the connection pool, even though we
    were calling both .close and .dispose. I'm afraid I dont have any more
    details to hand, but I do know one of our developers spent a week on the
    problem, eventually giving in and turning off connection pooling on the
    server.

    Has anyone come across this?

    Thanks again,
    Simon.
     
    Simon Harris, May 31, 2005
    #4
  5. Simon Harris

    Karl Seguin Guest

    If you had integrated security (windows login) and windows authentication
    on, I'd expect it to happen. Else I wouldn't know without further detail.

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Simon Harris" <> wrote in message
    news:...
    > Thank you both for your answers. I think I'll just use dispose from now
    > on.
    >
    > Whilst were on the subject, we had a situation recently where an
    > application was not releasing connections back into the connection pool,
    > even though we were calling both .close and .dispose. I'm afraid I dont
    > have any more details to hand, but I do know one of our developers spent a
    > week on the problem, eventually giving in and turning off connection
    > pooling on the server.
    >
    > Has anyone come across this?
    >
    > Thanks again,
    > Simon.
    >
     
    Karl Seguin, Jun 1, 2005
    #5
  6. Hi,

    Yes, we are using integrated security (It's an Intranet). How is this
    related?

    Thanks.


    Karl Seguin wrote:
    > If you had integrated security (windows login) and windows authentication
    > on, I'd expect it to happen. Else I wouldn't know without further detail.
    >
    > Karl
     
    Web Team @ Borough of Poole, Jun 1, 2005
    #6
  7. Simon Harris

    Karl Seguin Guest

    Check out:
    http://weblogs.asp.net/sjoseph/archive/2005/03/23/395601.aspx

    and do a search for "integrated security" ...connections are pooled based on
    their connnection string, and w/integrated security the connection string is
    different per user, hence no pooling happens cross-user. My knowledge of
    this is very limited, just what I've read in passing...not sure if it's the
    problem you saw, but it's the only thing I know..

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "Web Team @ Borough of Poole" <> wrote in message
    news:...
    > Hi,
    >
    > Yes, we are using integrated security (It's an Intranet). How is this
    > related?
    >
    > Thanks.
    >
    >
    > Karl Seguin wrote:
    >> If you had integrated security (windows login) and windows authentication
    >> on, I'd expect it to happen. Else I wouldn't know without further detail.
    >>
    >> Karl

    >
     
    Karl Seguin, Jun 1, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ante Perkovic

    Re: reading sqldatareader after conn.close()

    Ante Perkovic, Jul 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    434
    Ante Perkovic
    Jul 2, 2003
  2. SS
    Replies:
    2
    Views:
    481
    Trond-Eirik Kolloen
    Jul 3, 2003
  3. Antonio Concepcion
    Replies:
    3
    Views:
    2,849
    Antonio Concepcion
    Feb 17, 2005
  4. FC
    Replies:
    3
    Views:
    4,245
  5. JTL.zheng
    Replies:
    6
    Views:
    435
    JTL.zheng
    Nov 11, 2007
Loading...

Share This Page