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 / ASP.NET / General / December 2007

Tip: Looking for answers? Try searching our database.

Very quick question: What's the difference between Close() and     Dispose()?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gnewsgroup - 18 Dec 2007 15:29 GMT
You know, a SqlConnection object, a SqlCommand object, a SqlDataReader
object and many other data access objects, have a Close() method and a
Dispose() method.

What are their differences?

Q1: Should we call both Close() and Dispose() on an object in finally
block?  If not, then which one should we call and why?

Q2: If it makes sense to call both, does the order of calling them
matter?

Q3: Does the order of calling either Close() or Dispose() or both
matter on a SqlConnection object, a SqlCommand object which contains
this connection, a SqlDataReader object which is a result of
ExecuteReader of this SqlCommand object?

Thank you.
Mark Rae [MVP] - 18 Dec 2007 15:48 GMT
> What are their differences?

Close() closes the connection and returns it to the pool, leaving the object
in memory until the garbage collector decides it can destroy it - Dispose()
removes it from memory by first calling Close:
http://blog.devstone.com/aaron/archive/2004/06/03/184.aspx

> Q1: Should we call both Close() and Dispose() on an object in finally
> block?

No.

> If not, then which one should we call and why?

Neither - use the "using (.......) {.....}" syntax as described in the above
article.

> Q2: If it makes sense to call both, does the order of calling them
> matter?

See above - Dispose calls Close anyway...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

gnewsgroup - 18 Dec 2007 16:25 GMT
> > What are their differences?
>
> Close() closes the connection and returns it to the pool, leaving the object
> in memory until the garbage collector decides it can destroy it - Dispose()
> removes it from memory by first calling Close:http://blog.devstone.com/aaron/archive/2004/06/03/184.aspx

Thank you.  So, I guess if the connection object is gonna be used
soon, we better simply call Close() to improve the performance.

> > Q1: Should we call both Close() and Dispose() on an object in finally
> > block?
[quoted text clipped - 5 lines]
> Neither - use the "using (.......) {.....}" syntax as described in the above
> article.

I am aware of the using syntax, but have never seriously used it.

I read the article which you referred to above. Very nice, but I am
sorta confused by this:

<quote>
When Close() is called (or Dispose() by virtue of what it does) the
connection is not actually closed but is returned to the database
connection pool - remember that the connection pool is a collection of
SQL Connections, not SqlConnection objects.
</quote>

Does the author imply (by way of the parenthesized part) that Close()
and Dispose makes no difference in terms of returning the connection
object to the conection pool?
Mark Rae [MVP] - 18 Dec 2007 16:47 GMT
> Thank you.  So, I guess if the connection object is gonna be used
> soon, we better simply call Close() to improve the performance.

No - use the using syntax...

> I am aware of the using syntax, but have never seriously used it.

I recommend that you do...

> I read the article which you referred to above. Very nice, but I am
> sorta confused by this:
[quoted text clipped - 9 lines]
> and Dispose makes no difference in terms of returning the connection
> object to the conection pool?

Yes.

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Cowboy (Gregory A. Beamer) - 18 Dec 2007 16:45 GMT
> "gnewsgroup" <gnewsgroup@gmail.com> wrote in message
> Neither - use the "using (.......) {.....}" syntax as described in the
> above article.

This is a bit misleading, as the following are functionally equivalent:

using(SqlConnection conn = new SqlConnection())
{
   // work here
}

SqlConnection conn = new SqlConnection();
try
{
   conn.Open();
   // work here
}
finally
{
   conn.Dispose();
}

The main advantage of using, here, is it is more succinct and therefore
faster to code. The advantage of the try ... finally is you have the option
of adding a catch without recoding the entire instantiation and use.

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************

| Think outside the box!

*************************************************
Mark Rae [MVP] - 18 Dec 2007 16:51 GMT
> This is a bit misleading, as the following are functionally equivalent:

True.

> The main advantage of using, here, is it is more succinct and therefore
> faster to code.

Indeed, which is why I recommended it... :-)

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Cowboy (Gregory A. Beamer) - 18 Dec 2007 16:59 GMT
>> This is a bit misleading, as the following are functionally equivalent:
>
[quoted text clipped - 4 lines]
>
> Indeed, which is why I recommended it... :-)

I find too many cases where I temporary have to add a catch, so I am a
creature of habit. ;-)

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************

| Think outside the box!

*************************************************
Mark Rae [MVP] - 18 Dec 2007 17:05 GMT
> I find too many cases where I temporary have to add a catch, so I am a
> creature of habit. ;-)

Yeah, me too...

I have a DAL that I use for everything, so I don't really need to think
about this on a daily basis...

Signature

Mark Rae
ASP.NET MVP
http://www.markrae.net

Cowboy (Gregory A. Beamer) - 18 Dec 2007 16:59 GMT
> You know, a SqlConnection object, a SqlCommand object, a SqlDataReader
> object and many other data access objects, have a Close() method and a
[quoted text clipped - 4 lines]
> Q1: Should we call both Close() and Dispose() on an object in finally
> block?  If not, then which one should we call and why?

I generally use this pattern:

try
{
   conn.Open();
   //work here
}
finally
{
   conn.Dispose();
}

The Dispose() will call Close on the object and mark it for cleanup. As
connections are returned to the pool, for reuse, they are dead from your
perspective, but still have an underlying lifetime. Not a big deal.

The other option is using:

using(SqlConnection conn = new SqlConnection())
{
   //work here
}

This automatically disposes the object for you and makes for very clean
syntax. I generally go to the extra work of setting up the try ... finally,
however, as it allows me to add a catch.

> Q2: If it makes sense to call both, does the order of calling them
> matter?

It doesn't make sense, but you have to call Close() first if you do.

> Q3: Does the order of calling either Close() or Dispose() or both
> matter on a SqlConnection object, a SqlCommand object which contains
> this connection, a SqlDataReader object which is a result of
> ExecuteReader of this SqlCommand object?

Try to clean up any objects as they are on the stack. With the command,
there really is no clean up as killing connection will solve it. You have
the option on the DataReader, if you so desire.

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************

| Think outside the box!

*************************************************

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.