HighTechTalks DotNet Forums  

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

ASP.net ASP.net discussions (microsoft.public.dotnet.framework.aspnet)


Discuss Very quick question: What's the difference between Close() andDispose()? in the ASP.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
gnewsgroup
 
Posts: n/a

Default Very quick question: What's the difference between Close() andDispose()? - 12-18-2007 , 10:29 AM






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.

Reply With Quote
  #2  
Old   
Mark Rae [MVP]
 
Posts: n/a

Default Re: Very quick question: What's the difference between Close() and Dispose()? - 12-18-2007 , 10:48 AM






"gnewsgroup" <gnewsgroup (AT) gmail (DOT) com> wrote


Quote:
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/archi...06/03/184.aspx

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

Quote:
If not, then which one should we call and why?
Neither - use the "using (.......) {.....}" syntax as described in the above
article.

Quote:
Q2: If it makes sense to call both, does the order of calling them
matter?
See above - Dispose calls Close anyway...


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



Reply With Quote
  #3  
Old   
gnewsgroup
 
Posts: n/a

Default Re: Very quick question: What's the difference between Close() andDispose()? - 12-18-2007 , 11:25 AM



On Dec 18, 10:48 am, "Mark Rae [MVP]" <m... (AT) markNOSPAMrae (DOT) net> wrote:
Quote:
"gnewsgroup" <gnewsgr... (AT) gmail (DOT) com> wrote in message

news:30d32f99-aed8-4bcb-a585-405b4e86ad4d (AT) s8g2000prg (DOT) googlegroups.com...

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/archi...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.

Quote:
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.

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?


Reply With Quote
  #4  
Old   
Cowboy \(Gregory A. Beamer\)
 
Posts: n/a

Default Re: Very quick question: What's the difference between Close() and Dispose()? - 12-18-2007 , 11:45 AM




"Mark Rae [MVP]" <mark (AT) markNOSPAMrae (DOT) net> wrote

Quote:
"gnewsgroup" <gnewsgroup (AT) gmail (DOT) 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.

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

*************************************************
Quote:
Think outside the box!

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




Reply With Quote
  #5  
Old   
Mark Rae [MVP]
 
Posts: n/a

Default Re: Very quick question: What's the difference between Close() and Dispose()? - 12-18-2007 , 11:47 AM



"gnewsgroup" <gnewsgroup (AT) gmail (DOT) com> wrote


Quote:
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...

Quote:
I am aware of the using syntax, but have never seriously used it.
I recommend that you do...

Quote:
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?
Yes.


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



Reply With Quote
  #6  
Old   
Mark Rae [MVP]
 
Posts: n/a

Default Re: Very quick question: What's the difference between Close() and Dispose()? - 12-18-2007 , 11:51 AM



"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld (AT) comcast (DOT) netNoSpamM> wrote in
message news:uvP4vVZQIHA.5524 (AT) TK2MSFTNGP05 (DOT) phx.gbl...

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

Quote:
The main advantage of using, here, is it is more succinct and therefore
faster to code.
Indeed, which is why I recommended it... :-)


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



Reply With Quote
  #7  
Old   
Cowboy \(Gregory A. Beamer\)
 
Posts: n/a

Default Re: Very quick question: What's the difference between Close() and Dispose()? - 12-18-2007 , 11:59 AM




"gnewsgroup" <gnewsgroup (AT) gmail (DOT) com> wrote

Quote:
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?
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.

Quote:
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.

Quote:
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.

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

*************************************************
Quote:
Think outside the box!

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




Reply With Quote
  #8  
Old   
Cowboy \(Gregory A. Beamer\)
 
Posts: n/a

Default Re: Very quick question: What's the difference between Close() and Dispose()? - 12-18-2007 , 11:59 AM




"Mark Rae [MVP]" <mark (AT) markNOSPAMrae (DOT) net> wrote

Quote:
"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld (AT) comcast (DOT) netNoSpamM> wrote in
message news:uvP4vVZQIHA.5524 (AT) TK2MSFTNGP05 (DOT) phx.gbl...

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... :-)

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

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

*************************************************
Quote:
Think outside the box!

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




Reply With Quote
  #9  
Old   
Mark Rae [MVP]
 
Posts: n/a

Default Re: Very quick question: What's the difference between Close() and Dispose()? - 12-18-2007 , 12:05 PM



"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld (AT) comcast (DOT) netNoSpamM> wrote in
message news:uWMBwdZQIHA.3940 (AT) TK2MSFTNGP05 (DOT) phx.gbl...

Quote:
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...


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



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.