Should I close a connection in a dispose method?

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Should I close a connection in a dispose method?

    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 Guest

  2. Similar Questions and Discussions

    1. close a connection
      Hello everyone, I want to know how I force a close connection, i´ve made a logout buttom but if the user refeuse to use it how the list of users...
    2. #39569 [NEW]: OCI_Close doesn't close the connection.
      From: mc_hades at yahoo dot com Operating system: HP-UX PHP version: 5.2.0 PHP Bug Type: OCI8 related Bug description: ...
    3. net::sftp connection close
      Hi, I have a strange connection problem with sftp. It seems that connection is establish but when I try to download any file it return me an...
    4. connection remains active even if i close it !?!?
      i have the following problem. i have a video chat application and when multiple clients connects to the app let's say : chat/motor and...
    5. Does close connection work placed after redirect?
      All, I have a question about closeting a connection. If you put the "connection.Close" statement on a line below a redirect does it ever run?...
  3. #2

    Default Re: Should I close a connection in a dispose method?

    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
    [url]http://www.takempis.com[/url]
    Some things just happen.
    Everything else occurs.

    "SS" <stephen@acsalaska.com> wrote in message
    news:%23IrKOfTQDHA.3016@TK2MSFTNGP10.phx.gbl...
    > 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 Guest

  4. #3

    Default Re: Should I close a connection in a dispose method?

    inline...

    "SS" <stephen@acsalaska.com> wrote in message
    news:%23IrKOfTQDHA.3016@TK2MSFTNGP10.phx.gbl...
    > 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?

    No, it does not sound like correct thinking.

    You never know when your distructor is called (except if you implement the
    IDisposable interface, and if you do so, your in the mercy of your callers)
    This is because you do not know when the GC will do it's stuff, and you may
    risk to get A LOT of open connections, witch really is something you do not
    want.

    As .NET connection automatically uses connection pooling, you do not
    actually open and close your connection tree time, you just put's it back
    into the connection pool (wich is a quite cheep operation regarding process
    cycles)

    The minimal advantages you might gain by keeping the connection open does
    not defend the higher risk of using your suggested method.

    You should also remember that if you want your class to scale as good as
    possible, you should keep it statless. If you hold on to an open connection,
    it's not statless....

    Regards, TEK
    > 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!!
    >
    >

    Trond-Eirik Kolloen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139