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

G

gnewsgroup

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.
 
M

Mark Rae [MVP]

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...
 
G

gnewsgroup

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.
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?
 
C

Cowboy \(Gregory A. Beamer\)

Mark Rae said:
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

*************************************************
| Think outside the box!
|
*************************************************
 
M

Mark Rae [MVP]

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:

<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.
 
M

Mark Rae [MVP]

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

Cowboy \(Gregory A. Beamer\)

gnewsgroup said:
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.
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.

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

*************************************************
| Think outside the box!
|
*************************************************
 
C

Cowboy \(Gregory A. Beamer\)

Mark Rae said:
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

*************************************************
| Think outside the box!
|
*************************************************
 
M

Mark Rae [MVP]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top