PC Review


Reply
Thread Tools Rate Thread

Correct handling of Connection.Dispose() and Close()

 
 
_DG
Guest
Posts: n/a
 
      9th Nov 2005
There seems to be some disagreement on the subject of disposing vs
closing connections, and exactly when to do that. I have an app that
repeatedly refers to the same database. Each access involves only a
few lines of code, but I understand that it is still good to at least
*close* the connection after each access.

Q1: Given the short duration of each access and the fact that the
same db is being constantly reopened, does it make sense to just close
rather than dispose? (Does dispose impair pooling operation?)

Q2: What is the preferred method for enclosing access statements to
ensure that close (or dispose) is called? A short sample in C# would
be helpful.

 
Reply With Quote
 
 
 
 
Sahil Malik [MVP]
Guest
Posts: n/a
 
      9th Nov 2005
> Q1: Given the short duration of each access and the fact that the
> same db is being constantly reopened, does it make sense to just close
> rather than dispose? (Does dispose impair pooling operation?)


Close is allright, dispose does not impair pooling operation, but it does
clear stateful information out of the connection variable.

> Q2: What is the preferred method for enclosing access statements to
> ensure that close (or dispose) is called? A short sample in C# would
> be helpful.


Either a using block, or a try catch finally ~ with dispose in the finally.

--

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.ma.../13/63199.aspx
-------------------------------------------------------------------------------------------




"_DG" <_(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> There seems to be some disagreement on the subject of disposing vs
> closing connections, and exactly when to do that. I have an app that
> repeatedly refers to the same database. Each access involves only a
> few lines of code, but I understand that it is still good to at least
> *close* the connection after each access.
>
> Q1: Given the short duration of each access and the fact that the
> same db is being constantly reopened, does it make sense to just close
> rather than dispose? (Does dispose impair pooling operation?)
>
> Q2: What is the preferred method for enclosing access statements to
> ensure that close (or dispose) is called? A short sample in C# would
> be helpful.
>




 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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?= Microsoft ASP .NET 8 18th Oct 2004 10:31 AM
SqlConnection.Dispose close connection? Big D Microsoft C# .NET 13 23rd Mar 2004 06:50 AM
Do DataAdapter's dispose close the connection? poseidon Microsoft ADO .NET 1 7th Feb 2004 05:31 PM
Should I close a connection in a dispose method? SS Microsoft Dot NET Framework 2 3rd Jul 2003 07:30 PM
Should I close a connection in a dispose method? SS Microsoft ASP .NET 2 3rd Jul 2003 07:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:26 PM.