Are Close and Dispose on connection objects equivilent?

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

I read there is no difference between calling Close vs. Dispose on
Connection objects.

Is this true?
 
Dispose closes the connection and discards the object. It has no further
effect. It does not impact the connection pool.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Bill,

Dispose does not "discard the object".
See http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx

Close() and Dispose() should be equivalent. See http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconfinalizedispose.asp for more information.

-Chris

--------------------
Dispose closes the connection and discards the object. It has no further
effect. It does not impact the connection pool.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Okay, let me rephrase. Once you call Dispose, you can't use the object's
properties or methods. The object remains in memory until the GC flushes its
memory... but it's "discarded" as far as your code is concerned. Close
simply marks the connection as reusable (in the pool) and you can
subsequently inspect Connection properties and use the Open method again.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Chris Lyon [MSFT]" said:
Bill,

Dispose does not "discard the object".
See http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx

Close() and Dispose() should be equivalent. See
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconfinalizedispose.asp
for more information.

-Chris

--------------------
Dispose closes the connection and discards the object. It has no further
effect. It does not impact the connection pool.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________


--

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Bill,

Calling Dispose() doesn't then prohibit you from calling the object's properties and methods. See this blog post, in particular the comments:
http://weblogs.asp.net/clyon/archive/2004/09/23/233464.aspx

If the class' developer followed the Dispose Pattern (link below) correctly, then Close and Dispose are equivalent.

For example, a SQLConnection object will have the same behaviour after calling Close or Dispose. The same InvalidOperationExceptions will be thrown when some methods
(like ChangeDatabase()) are called, but not all.

-Chris

--------------------
Okay, let me rephrase. Once you call Dispose, you can't use the object's
properties or methods. The object remains in memory until the GC flushes its
memory... but it's "discarded" as far as your code is concerned. Close
simply marks the connection as reusable (in the pool) and you can
subsequently inspect Connection properties and use the Open method again.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Chris Lyon [MSFT]" said:
Bill,

Dispose does not "discard the object".
See http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx

Close() and Dispose() should be equivalent. See
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconfinalizedispose.asp
for more information.

-Chris

--------------------
Dispose closes the connection and discards the object. It has no further
effect. It does not impact the connection pool.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

I read there is no difference between calling Close vs. Dispose on
Connection objects.

Is this true?


--

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Chris,

Here is reflected code out of SqlConnection.Dispose ---

protected override void Dispose(bool disposing)
{
switch (this._objectState)
{
case ConnectionState.Open:
{
goto Label_0017;
}
}
goto Label_0035;
Label_0017:
if (disposing)
{
this.Close();
this._connectionOptions = null;
this._cachedOptions = null;
this._connectionString = null;
}
Label_0035:
base.Dispose(disposing);
}

You can clearly see Dispose does more than Close, it sets certain properties
to null. .. therefore Dispose is not the same as Close.

I think Bill's right dude :-/

- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik



"Chris Lyon [MSFT]" said:
Bill,

Calling Dispose() doesn't then prohibit you from calling the object's
properties and methods. See this blog post, in particular the comments:
http://weblogs.asp.net/clyon/archive/2004/09/23/233464.aspx

If the class' developer followed the Dispose Pattern (link below)
correctly, then Close and Dispose are equivalent.
For example, a SQLConnection object will have the same behaviour after
calling Close or Dispose. The same InvalidOperationExceptions will be
thrown when some methods
(like ChangeDatabase()) are called, but not all.

-Chris

--------------------
Okay, let me rephrase. Once you call Dispose, you can't use the object's
properties or methods. The object remains in memory until the GC flushes its
memory... but it's "discarded" as far as your code is concerned. Close
simply marks the connection as reusable (in the pool) and you can
subsequently inspect Connection properties and use the Open method again.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Chris Lyon [MSFT]" said:
Bill,

Dispose does not "discard the object".
See http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx

Close() and Dispose() should be equivalent. See
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconfinalizedispose.asp
for more information.

-Chris

--------------------


Dispose closes the connection and discards the object. It has no further
effect. It does not impact the connection pool.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

I read there is no difference between calling Close vs. Dispose on
Connection objects.

Is this true?







--

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
I stand corrected. When I saw that my opens failed post dispose I took it to
mean the object was missing--it was simply cleared. Learn something new
every day. Thanks

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

"Chris Lyon [MSFT]" said:
Bill,

Calling Dispose() doesn't then prohibit you from calling the object's
properties and methods. See this blog post, in particular the comments:
http://weblogs.asp.net/clyon/archive/2004/09/23/233464.aspx

If the class' developer followed the Dispose Pattern (link below)
correctly, then Close and Dispose are equivalent.

For example, a SQLConnection object will have the same behaviour after
calling Close or Dispose. The same InvalidOperationExceptions will be
thrown when some methods
(like ChangeDatabase()) are called, but not all.

-Chris

--------------------
Okay, let me rephrase. Once you call Dispose, you can't use the object's
properties or methods. The object remains in memory until the GC flushes
its
memory... but it's "discarded" as far as your code is concerned. Close
simply marks the connection as reusable (in the pool) and you can
subsequently inspect Connection properties and use the Open method again.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

"Chris Lyon [MSFT]" said:
Bill,

Dispose does not "discard the object".
See http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx

Close() and Dispose() should be equivalent. See
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconfinalizedispose.asp
for more information.

-Chris

--------------------


Dispose closes the connection and discards the object. It has no further
effect. It does not impact the connection pool.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

I read there is no difference between calling Close vs. Dispose on
Connection objects.

Is this true?







--

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms
specified
at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.


--

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Hm. Interesting.

According to the Dispose Pattern, Close should call Dispose. It looks like in this case a little more cleanup is happening. I'm not familiar enough with SQLConnection to know
what kind of effects this might have on your object, or whether it's appropriate to continue to use a SQLConnection object after it's been closed.

Close and Dispose are equivalent, in the sense that when you're done with a connection object, calling either Close or Dispose is sufficient to free your unmanaged resource.

-Chris


--------------------
Chris,

Here is reflected code out of SqlConnection.Dispose ---

protected override void Dispose(bool disposing)
{
switch (this._objectState)
{
case ConnectionState.Open:
{
goto Label_0017;
}
}
goto Label_0035;
Label_0017:
if (disposing)
{
this.Close();
this._connectionOptions = null;
this._cachedOptions = null;
this._connectionString = null;
}
Label_0035:
base.Dispose(disposing);
}

You can clearly see Dispose does more than Close, it sets certain properties
to null. .. therefore Dispose is not the same as Close.

I think Bill's right dude :-/

- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik



"Chris Lyon [MSFT]" said:
Bill,

Calling Dispose() doesn't then prohibit you from calling the object's
properties and methods. See this blog post, in particular the comments:
http://weblogs.asp.net/clyon/archive/2004/09/23/233464.aspx

If the class' developer followed the Dispose Pattern (link below)
correctly, then Close and Dispose are equivalent.
For example, a SQLConnection object will have the same behaviour after
calling Close or Dispose. The same InvalidOperationExceptions will be
thrown when some methods
(like ChangeDatabase()) are called, but not all.

-Chris
rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
Close and Dispose are equivalent, in the sense that when you're done with
a connection object, calling either Close or Dispose is sufficient to free
your unmanaged resource.

That I totally agree with :-) ... havagudday !! (me leavin' work will be in
dotnetrocks tonight - chatroom .. see ya there).

- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik


"Chris Lyon [MSFT]" said:
Hm. Interesting.

According to the Dispose Pattern, Close should call Dispose. It looks
like in this case a little more cleanup is happening. I'm not familiar
enough with SQLConnection to know
what kind of effects this might have on your object, or whether it's
appropriate to continue to use a SQLConnection object after it's been
closed.
Close and Dispose are equivalent, in the sense that when you're done with
a connection object, calling either Close or Dispose is sufficient to free
your unmanaged resource.
-Chris


--------------------
Chris,

Here is reflected code out of SqlConnection.Dispose ---

protected override void Dispose(bool disposing)
{
switch (this._objectState)
{
case ConnectionState.Open:
{
goto Label_0017;
}
}
goto Label_0035;
Label_0017:
if (disposing)
{
this.Close();
this._connectionOptions = null;
this._cachedOptions = null;
this._connectionString = null;
}
Label_0035:
base.Dispose(disposing);
}

You can clearly see Dispose does more than Close, it sets certain properties
to null. .. therefore Dispose is not the same as Close.

I think Bill's right dude :-/

- Sahil Malik
You can reach me thru my blog at
http://www.dotnetjunkies.com/weblog/sahilmalik



"Chris Lyon [MSFT]" said:
Bill,

Calling Dispose() doesn't then prohibit you from calling the object's
properties and methods. See this blog post, in particular the comments:
http://weblogs.asp.net/clyon/archive/2004/09/23/233464.aspx

If the class' developer followed the Dispose Pattern (link below)
correctly, then Close and Dispose are equivalent.
For example, a SQLConnection object will have the same behaviour after
calling Close or Dispose. The same InvalidOperationExceptions will be
thrown when some methods
(like ChangeDatabase()) are called, but not all.

-Chris

--------------------


Okay, let me rephrase. Once you call Dispose, you can't use the object's
properties or methods. The object remains in memory until the GC
flushes
its
memory... but it's "discarded" as far as your code is concerned. Close
simply marks the connection as reusable (in the pool) and you can
subsequently inspect Connection properties and use the Open method again.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________

Bill,

Dispose does not "discard the object".
See http://weblogs.asp.net/clyon/archive/2004/09/21/232445.aspx

Close() and Dispose() should be equivalent. See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/h tml/cpconfinalizedispose.asp
for more information.

-Chris

--------------------


Dispose closes the connection and discards the object. It has no further
effect. It does not impact the connection pool.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no
rights.
__________________________________

I read there is no difference between calling Close vs. Dispose on
Connection objects.

Is this true?







--

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Back
Top