473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Should I close a connection in a dispose method?

SS
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!!
Nov 17 '05 #1
2 3015
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" <st*****@acsalaska.com> wrote in message
news:%2****************@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!!

Nov 17 '05 #2
inline...

"SS" <st*****@acsalaska.com> wrote in message
news:%2****************@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!!

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Henrik Dahl | last post by:
Hello! After I've finished using an instance of the SqlCommand class, should I then invoke Dispose() on the instance. I suppose so, as there is a Dispose method, but what does it actually...
10
by: Jim H | last post by:
I sometimes get the following error from my Form's Dispose Method when the application is closing: ------------------------------------------------- An unhandled exception of type...
5
by: ypul | last post by:
the code below given is connection class ... now I want to use the connection in another class , by using the getConnection method. where should I call con.dispose() ? in connection class or...
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
3
by: Ed | last post by:
I just learned how to use the Commandbuilder and have researched it a little bit further and see that there is a dispose property. Should this be used after invoking the command builder or does...
0
by: bonita | last post by:
In my ASP.NET page, I have 2 checkboxes for users to choose which crystal report they want to display. These two reports use different tables. If report1 has been choosen and displayed in the...
46
by: clintonG | last post by:
Documentation tells me how but not when, why or where... <%= Clinton Gallagher http://msdn2.microsoft.com/en-us/library/saxz13w4(VS.80).aspx
2
by: Michael Carr | last post by:
I recently discovered the "using" C# keyword and the neat and tidy way it guarantees cleanup of resources. However, how far should I take this? I recall some discussion from Microsoft suggesting...
6
by: S_K | last post by:
Hi all! I'm writing an ASP.NET web application that uses an Oracle database. I OPEN the Oracle connection by using the following code: if (this.ConnectionString != "") { this.Connection =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.