Home All Groups Group Topic Archive Search About

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

Author
9 Nov 2005 10:42 AM
_DG
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.

Author
9 Nov 2005 11:07 AM
Sahil Malik [MVP]
> 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.malik/archive/2005/05/13/63199.aspx
-------------------------------------------------------------------------------------------




Show quote
"_DG" <_**@nomail.com> wrote in message
news:qvj3n1p4hdrpdgjha0hh60n90qj76l66dm@4ax.com...
> 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.
>

AddThis Social Bookmark Button