Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Should I close a connection in a dispose method?

Reply
Thread Tools

Should I close a connection in a dispose method?

 
 
SS
Guest
Posts: n/a
 
      07-03-2003
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!!


 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      07-03-2003
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!!
>
>



 
Reply With Quote
 
 
 
 
Duray AKAR
Guest
Posts: n/a
 
      07-03-2003
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!!
>
>
>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection Pooling, Dispose/Close/Using =?Utf-8?B?UGllcnNvbiBD?= ASP .Net 9 11-26-2008 02:58 PM
Conn.Close & Conn.Dispose Simon Harris ASP .Net 6 06-01-2005 01:01 PM
where should I dispose the connection ? ypul ASP .Net 5 02-28-2005 12:41 PM
use of MySQLConnection.dispose and/or MySQLCommand.dispose Antonio Concepcion ASP .Net 3 02-17-2005 06:33 PM
Should I close a connection in a dispose method? SS ASP .Net 2 07-03-2003 06:30 PM



Advertisments