Should I close a connection in a dispose method?

Discussion in 'ASP .Net Web Controls' started by SS, Jul 3, 2003.

  1. SS

    SS Guest

    I've built a class to broker the data in may application. In other words,
    when my business logic needs a list of widgets in the db, it calls
    mybrokerclass.getWidgetList, which might return a collection or arraylist of
    widgets.

    This way, the business logic doesn't care about where the data comes from.
    (BTW, this is a web application)

    So my problem is this.. during a postback, a series of operations will occur
    wherein perhaps three requests to this broker class will take place, all for
    different sets of data. Rather than opening and closing the connection to
    the db in each method (three times opened, three times closed in this case),
    I think it would be better to OPEN the connection in the constructor of the
    class and then put the CLOSE method in the destructor (dispose). Does this
    sound like correct thinking or am I screwed up in how I'm approaching this?

    P.S., since I'm only worried about closing the db connection in this dispose
    method, I'm assuming I should _not_ call the GC.suppressFinalize method so
    the GC can clean up anything remaining...?

    Thanks for any help on this!!
     
    SS, Jul 3, 2003
    #1
    1. Advertising

  2. It can get complicated. For example, if your class works with DataReaders,
    only 1 DataReader at a time can be associated with an opened Connection.
    Also, is this a static or instance class? If it is a static class, your
    Connection will never be closed. In fact, as ADO.Net leverages Connection
    Pooling very well, you would probably do just as well to simply open and
    close the Connection as quickly as possible.

    HTH,

    Kevin Spencer
    Microsoft FrontPage MVP
    Internet Developer
    http://www.takempis.com
    Some things just happen.
    Everything else occurs.

    "SS" <> wrote in message
    news:%...
    > I've built a class to broker the data in may application. In other words,
    > when my business logic needs a list of widgets in the db, it calls
    > mybrokerclass.getWidgetList, which might return a collection or arraylist

    of
    > widgets.
    >
    > This way, the business logic doesn't care about where the data comes from.
    > (BTW, this is a web application)
    >
    > So my problem is this.. during a postback, a series of operations will

    occur
    > wherein perhaps three requests to this broker class will take place, all

    for
    > different sets of data. Rather than opening and closing the connection to
    > the db in each method (three times opened, three times closed in this

    case),
    > I think it would be better to OPEN the connection in the constructor of

    the
    > class and then put the CLOSE method in the destructor (dispose). Does this
    > sound like correct thinking or am I screwed up in how I'm approaching

    this?
    >
    > P.S., since I'm only worried about closing the db connection in this

    dispose
    > method, I'm assuming I should _not_ call the GC.suppressFinalize method so
    > the GC can clean up anything remaining...?
    >
    > Thanks for any help on this!!
    >
    >
     
    Kevin Spencer, Jul 3, 2003
    #2
    1. Advertising

  3. SS

    Duray AKAR Guest

    It sounds like you are using DataReader since you are
    already concerned with opening and closing connections...

    Just open the connections when you need, and close it as
    soon as you are done, ADO.NET takes care of the pooling of
    the connections very well. The pools are maintained per
    connection string (including the username and password,
    the full string)... You can set the size of the pool using
    ADO.NET configuration methods, to make advanced tuning on
    the performance, but just in extreme cases...(e.g. you
    have a connection license for onyl 5 connections to the
    datasource, and you want to keep one of them for
    administrative access, e.t.c.) Supressiing finalize, or
    trying to manually clean them up will not help at all...

    Btw, since you are using a web application,
    what is the scope of your broker class? Are those all
    static methods that you call?



    Duray AKAR

    >-----Original Message-----
    >I've built a class to broker the data in may application.

    In other words,
    >when my business logic needs a list of widgets in the db,

    it calls
    >mybrokerclass.getWidgetList, which might return a

    collection or arraylist of
    >widgets.
    >
    >This way, the business logic doesn't care about where the

    data comes from.
    >(BTW, this is a web application)
    >
    >So my problem is this.. during a postback, a series of

    operations will occur
    >wherein perhaps three requests to this broker class will

    take place, all for
    >different sets of data. Rather than opening and closing

    the connection to
    >the db in each method (three times opened, three times

    closed in this case),
    >I think it would be better to OPEN the connection in the

    constructor of the
    >class and then put the CLOSE method in the destructor

    (dispose). Does this
    >sound like correct thinking or am I screwed up in how I'm

    approaching this?
    >
    >P.S., since I'm only worried about closing the db

    connection in this dispose
    >method, I'm assuming I should _not_ call the

    GC.suppressFinalize method so
    >the GC can clean up anything remaining...?
    >
    >Thanks for any help on this!!
    >
    >
    >.
    >
     
    Duray AKAR, Jul 3, 2003
    #3
    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. SS
    Replies:
    2
    Views:
    455
    Trond-Eirik Kolloen
    Jul 3, 2003
  2. =?Utf-8?B?UGllcnNvbiBD?=

    Connection Pooling, Dispose/Close/Using

    =?Utf-8?B?UGllcnNvbiBD?=, Oct 14, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    4,623
    speedy
    Nov 26, 2008
  3. Antonio Concepcion
    Replies:
    3
    Views:
    2,737
    Antonio Concepcion
    Feb 17, 2005
  4. ypul
    Replies:
    5
    Views:
    375
  5. Simon Harris

    Conn.Close & Conn.Dispose

    Simon Harris, May 31, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    5,977
    Karl Seguin
    Jun 1, 2005
Loading...

Share This Page