PC Review


Reply
Thread Tools Rate Thread

connection.dispose()

 
 
=?Utf-8?B?Q2h1YSBXZW4gQ2hpbmc=?=
Guest
Posts: n/a
 
      4th Jul 2004
Hi z.f.

connection.close the way to go.


--
Regards,
Chua Wen Ching


"z. f." wrote:

> must i call connection.dispose()
> or the close() method is enough,
> on my sql server i see many connections created.
>
> TIA, z.
>
>
>

 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      4th Jul 2004
Hi ZF,

connection.dispose
(It is one of the exceptions)

It works almost the same as the close however when you want to open the
connection again you have to add the connectionstring again. (Of course
instead of the close not extra)

Cor


 
Reply With Quote
 
z. f.
Guest
Posts: n/a
 
      4th Jul 2004
must i call connection.dispose()
or the close() method is enough,
on my sql server i see many connections created.

TIA, z.


 
Reply With Quote
 
Michael Giagnocavo [MVP]
Guest
Posts: n/a
 
      4th Jul 2004
The Dispose method mainly calls Close().

-mike
MVP

"Cor Ligthert" <(E-Mail Removed)> wrote in message
news:epm7%(E-Mail Removed)...
> Hi ZF,
>
> connection.dispose
> (It is one of the exceptions)
>
> It works almost the same as the close however when you want to open the
> connection again you have to add the connectionstring again. (Of course
> instead of the close not extra)
>
> Cor
>
>



 
Reply With Quote
 
Miha Markic [MVP C#]
Guest
Posts: n/a
 
      4th Jul 2004
When you want to destroy the connection instance, call Dispose. When you
want only to close the connection, call Close.
There is not much difference as Cor mentioned, however, in the future the
behaviour might change.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"z. f." <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> must i call connection.dispose()
> or the close() method is enough,
> on my sql server i see many connections created.
>
> TIA, z.
>
>



 
Reply With Quote
 
Daniel Billingsley
Guest
Posts: n/a
 
      7th Jul 2004
The unmanaged resource we're concerned about *is* the literal connection
though, right? By that I mean that any differences between Close and
Dispose are somewhat moot for practical resource management concerns, isn't
it?

Also, what if you just rely on the following code to close the connection?
Then technically you don't need to explicitly close or dispose the
connection object itself, right?
..ExecuteReader(CommandBehavior.CloseConnection)



"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:%(E-Mail Removed)...
> When you want to destroy the connection instance, call Dispose. When you
> want only to close the connection, call Close.
> There is not much difference as Cor mentioned, however, in the future the
> behaviour might change.
>
> --
> Miha Markic [MVP C#] - RightHand .NET consulting & development
> miha at rthand com



 
Reply With Quote
 
Pablo Castro [MS]
Guest
Posts: n/a
 
      7th Jul 2004
Strictly speaking, the SqlConnection object is just a managed object, so
it's no big deal by itself. However, one of it's internal objects manages
native resources (network connections, native buffers, etc.) which need to
be carefully tracked to make sure we don't leak them. That's why is so
important to call Close or Dispose on the connection, so the internal
objects are properly cleaned-up.

Using CommandBehavior.CloseConnection is a great option if you know that you
won't need the connection after processing the result-set. Keep in mind,
though, that in this case you have to make sure you close the DataReader;
it's as important as closing the connection, because we'll close the
connection when you close the reader if CloseConnection is specified.

--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.


"Daniel Billingsley" <dbillingsley@NO_durcon_SPAAMM.com> wrote in message
news:(E-Mail Removed)...
> The unmanaged resource we're concerned about *is* the literal connection
> though, right? By that I mean that any differences between Close and
> Dispose are somewhat moot for practical resource management concerns,

isn't
> it?
>
> Also, what if you just rely on the following code to close the connection?
> Then technically you don't need to explicitly close or dispose the
> connection object itself, right?
> .ExecuteReader(CommandBehavior.CloseConnection)
>
>
>
> "Miha Markic [MVP C#]" <miha at rthand com> wrote in message
> news:%(E-Mail Removed)...
> > When you want to destroy the connection instance, call Dispose. When you
> > want only to close the connection, call Close.
> > There is not much difference as Cor mentioned, however, in the future

the
> > behaviour might change.
> >
> > --
> > Miha Markic [MVP C#] - RightHand .NET consulting & development
> > miha at rthand com

>
>



 
Reply With Quote
 
Daniel Billingsley
Guest
Posts: n/a
 
      7th Jul 2004
Yes, but that's a general description of why there is a Dispose on some
objects in the first place, isn't it? The objects themselves are handled
fine by GC, the nasty unmanaged resources inside them notwithstanding -
hence the propensity for leaks. Correct?

Glad to hear the second answer.. in my case I created a data factory to
return a DataReader... I can't wrap the connection in a using inside the
factory class since the DataReader is of course useless once the connection
is closed. Yeah I'm depending on the next layer developer to Dispose the
DataReader, but that person is me. Actually, I'm returning a wrapper for
the DataReader, so this is probably a case where there's a strong argument
for making my wrapper class throw an exception in the Finalizer since proper
disposal is so important yet out of the control of the factory class.

"Pablo Castro [MS]" <(E-Mail Removed)> wrote in message
news:uE%(E-Mail Removed)...
> Strictly speaking, the SqlConnection object is just a managed object, so
> it's no big deal by itself. However, one of it's internal objects manages
> native resources (network connections, native buffers, etc.) which need to
> be carefully tracked to make sure we don't leak them. That's why is so
> important to call Close or Dispose on the connection, so the internal
> objects are properly cleaned-up.
>
> Using CommandBehavior.CloseConnection is a great option if you know that

you
> won't need the connection after processing the result-set. Keep in mind,
> though, that in this case you have to make sure you close the DataReader;
> it's as important as closing the connection, because we'll close the
> connection when you close the reader if CloseConnection is specified.
>
> --
> Pablo Castro
> Program Manager - ADO.NET Team
> Microsoft Corp.
>



 
Reply With Quote
 
Pablo Castro [MS]
Guest
Posts: n/a
 
      8th Jul 2004
Yes, you're right in the statement below regarding general pattern for
Dispose().

Regarding throwing an exception in the finalizer...I'm not sure that's a
good idea. Who's going to catch the exception? there is no user code on top
of your finalizer in the finalizer thread...you can log it somewhere or
something like that.

--
Pablo Castro
Program Manager - ADO.NET Team
Microsoft Corp.

This posting is provided "AS IS" with no warranties, and confers no rights.


"Daniel Billingsley" <dbillingsley@NO_durcon_SPAAMM.com> wrote in message
news:OIlKF%(E-Mail Removed)...
> Yes, but that's a general description of why there is a Dispose on some
> objects in the first place, isn't it? The objects themselves are handled
> fine by GC, the nasty unmanaged resources inside them notwithstanding -
> hence the propensity for leaks. Correct?
>
> Glad to hear the second answer.. in my case I created a data factory to
> return a DataReader... I can't wrap the connection in a using inside the
> factory class since the DataReader is of course useless once the

connection
> is closed. Yeah I'm depending on the next layer developer to Dispose the
> DataReader, but that person is me. Actually, I'm returning a wrapper for
> the DataReader, so this is probably a case where there's a strong argument
> for making my wrapper class throw an exception in the Finalizer since

proper
> disposal is so important yet out of the control of the factory class.
>
> "Pablo Castro [MS]" <(E-Mail Removed)> wrote in message
> news:uE%(E-Mail Removed)...
> > Strictly speaking, the SqlConnection object is just a managed object, so
> > it's no big deal by itself. However, one of it's internal objects

manages
> > native resources (network connections, native buffers, etc.) which need

to
> > be carefully tracked to make sure we don't leak them. That's why is so
> > important to call Close or Dispose on the connection, so the internal
> > objects are properly cleaned-up.
> >
> > Using CommandBehavior.CloseConnection is a great option if you know that

> you
> > won't need the connection after processing the result-set. Keep in mind,
> > though, that in this case you have to make sure you close the

DataReader;
> > it's as important as closing the connection, because we'll close the
> > connection when you close the reader if CloseConnection is specified.
> >
> > --
> > Pablo Castro
> > Program Manager - ADO.NET Team
> > Microsoft Corp.
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Connection.Dispose and the connection pool Frank Rizzo Microsoft C# .NET 4 3rd Jul 2006 09:34 PM
connection.close vs. connection.dispose Scott M. Microsoft ADO .NET 6 4th Mar 2006 02:04 AM
where should I dispose the connection ? ypul Microsoft ASP .NET 5 28th Feb 2005 12:41 PM
connection.dispose() =?Utf-8?B?Q2h1YSBXZW4gQ2hpbmc=?= Microsoft ADO .NET 8 8th Jul 2004 01:40 AM
SqlConnection.Dispose close connection? Big D Microsoft C# .NET 13 23rd Mar 2004 06:50 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:12 PM.