Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / December 2006

Tip: Looking for answers? Try searching our database.

Is neglecting Dispose in this case acceptable?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brad Wood - 07 Dec 2006 15:40 GMT
I have a persistence class with one dababase connection and no other member
objects that need management.  For reasons that are not necessary to go
into, I'd rather not implement Dispose in this class.  If I implement a
finalizer, and close my database connection there, I see only one problem,
i.e., there is a slight performance loss due to finalization queue issues.

- I don't think the perf issue matters due to the fact that I never create
more than one of these persistence objects.
- The database connection won't close quite as quickly, and will be closed
from a separate thread, but so what?  It does close...

Am I wrong?
David Browne - 07 Dec 2006 18:38 GMT
>I have a persistence class with one dababase connection and no other member
>objects that need management.  For reasons that are not necessary to go
[quoted text clipped - 6 lines]
> - The database connection won't close quite as quickly, and will be closed
> from a separate thread, but so what?  It does close...

First, you should implement IDisposable.  Whatever reasons you have that you
don't want to go into, you should get over them and implement IDisposable.

Second, don't use a Finalizer.  The database connection has a finalizer
already.

Third, your question really is: "Is is alright for my application to leak a
few database connections?"  The answer is yes, so long as you only leak one
connection, you should be fine, although it's still very bad design.  But if
the usage or design of your app changes, and you start leaking more database
connections, your application will eventually and unpredictably fail.

David
Ben Voigt - 07 Dec 2006 21:03 GMT
>>I have a persistence class with one dababase connection and no other
>>member objects that need management.  For reasons that are not necessary
[quoted text clipped - 11 lines]
> you don't want to go into, you should get over them and implement
> IDisposable.

I agree.  Even if you decide not to call Dispose on your object, implement
IDisposable to give yourself the flexibility later.

> Second, don't use a Finalizer.  The database connection has a finalizer
> already.

The reason is that if your finalizer calls the database's Dispose, you will
cause the database to be resurrected and its memory won't be freed until the
next GC pass.  Even worse, the database could have been finalized already,
and calling any method (even Dispose) on an object after its finalizer has
run is very bad.

> Third, your question really is: "Is is alright for my application to leak
> a few database connections?"  The answer is yes, so long as you only leak
[quoted text clipped - 4 lines]
>
> David
Brad Wood - 07 Dec 2006 21:46 GMT
> Even worse, the database could have been finalized already, and calling
> any method (even Dispose) on an object after its finalizer has run is very
> bad.

Thx.  This is the important reason I was missing.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.