PC Review


Reply
Thread Tools Rate Thread

2 ways to close a connection

 
 
Eric Sabine
Guest
Posts: n/a
 
      13th Apr 2004
In my Finally block, I was using cn.close (where cn is an ADO.NET
connection object, SQLConnection to be exact) and then I came across the
following in some microsoft code.

If Not cn Is Nothing Then
CType(cn, IDisposable).Dispose()
End If

I have to admit, I'm not sure what happens here. Will someone explain this
line of code (the middle one, not the if statement LOL) to me please?
[hopefully I won't get flamed for this :-) ]

Eric



 
Reply With Quote
 
 
 
 
Raymond Lewallen
Guest
Posts: n/a
 
      13th Apr 2004
Eric,

The connection is converted to a type IDisposable and then the Dispose
method is called to release unmanaged resources. When you call Dispose(),
all the necessary cleanup is done when the call returns instead of waiting
on the GC to do it when it feels it is necessary at an indeterminate point
in the future.

Raymond Lewallen

"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:#(E-Mail Removed)...
> In my Finally block, I was using cn.close (where cn is an ADO.NET
> connection object, SQLConnection to be exact) and then I came across the
> following in some microsoft code.
>
> If Not cn Is Nothing Then
> CType(cn, IDisposable).Dispose()
> End If
>
> I have to admit, I'm not sure what happens here. Will someone explain

this
> line of code (the middle one, not the if statement LOL) to me please?
> [hopefully I won't get flamed for this :-) ]
>
> Eric
>
>
>



 
Reply With Quote
 
Eric Sabine
Guest
Posts: n/a
 
      13th Apr 2004
Thank you. What you said makes sense. If I had used cn.close, I am simply
at the mercy of the garbage collector to clean it up when it wants. Here
are 2 questions back to you.

1) is it not better to let the GC manage the resouces, or should I override
it?
2) I notice that the SQLConnection object has its own Dispose method. Why
do you suppose it's not used here?

Eric



"Raymond Lewallen" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Eric,
>
> The connection is converted to a type IDisposable and then the Dispose
> method is called to release unmanaged resources. When you call Dispose(),
> all the necessary cleanup is done when the call returns instead of waiting
> on the GC to do it when it feels it is necessary at an indeterminate point
> in the future.
>
> Raymond Lewallen
>
> "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
> news:#(E-Mail Removed)...
> > In my Finally block, I was using cn.close (where cn is an ADO.NET
> > connection object, SQLConnection to be exact) and then I came across the
> > following in some microsoft code.
> >
> > If Not cn Is Nothing Then
> > CType(cn, IDisposable).Dispose()
> > End If
> >
> > I have to admit, I'm not sure what happens here. Will someone explain

> this
> > line of code (the middle one, not the if statement LOL) to me please?
> > [hopefully I won't get flamed for this :-) ]
> >
> > Eric
> >
> >
> >

>
>



 
Reply With Quote
 
Mike McIntyre [MVP]
Guest
Posts: n/a
 
      13th Apr 2004
CType(cn, IDisposable).Dispose()

Treat(an object, as an IDisposable interface).and call Dispose method on the
result.

If cn is a variable that points to an ADO.NET connection object the code
below will work because ADO.NET connection objects implement the IDisposable
interface's Dispose method as a public method you can call directly.

cn.Dispose()

--
Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com

"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:%(E-Mail Removed)...
> In my Finally block, I was using cn.close (where cn is an ADO.NET
> connection object, SQLConnection to be exact) and then I came across the
> following in some microsoft code.
>
> If Not cn Is Nothing Then
> CType(cn, IDisposable).Dispose()
> End If
>
> I have to admit, I'm not sure what happens here. Will someone explain

this
> line of code (the middle one, not the if statement LOL) to me please?
> [hopefully I won't get flamed for this :-) ]
>
> Eric
>
>
>



 
Reply With Quote
 
Mike McIntyre [MVP]
Guest
Posts: n/a
 
      13th Apr 2004
Unmanaged resources should me managed by you. Learn more at:

http://www.devcity.net/net/article.aspx?alias=gc_manage

--
Mike McIntyre
Visual Basic MVP
www.getdotnetcode.com


"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:(E-Mail Removed)...
> Thank you. What you said makes sense. If I had used cn.close, I am

simply
> at the mercy of the garbage collector to clean it up when it wants. Here
> are 2 questions back to you.
>
> 1) is it not better to let the GC manage the resouces, or should I

override
> it?
> 2) I notice that the SQLConnection object has its own Dispose method. Why
> do you suppose it's not used here?
>
> Eric
>
>
>
> "Raymond Lewallen" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
> > Eric,
> >
> > The connection is converted to a type IDisposable and then the Dispose
> > method is called to release unmanaged resources. When you call

Dispose(),
> > all the necessary cleanup is done when the call returns instead of

waiting
> > on the GC to do it when it feels it is necessary at an indeterminate

point
> > in the future.
> >
> > Raymond Lewallen
> >
> > "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
> > news:#(E-Mail Removed)...
> > > In my Finally block, I was using cn.close (where cn is an ADO.NET
> > > connection object, SQLConnection to be exact) and then I came across

the
> > > following in some microsoft code.
> > >
> > > If Not cn Is Nothing Then
> > > CType(cn, IDisposable).Dispose()
> > > End If
> > >
> > > I have to admit, I'm not sure what happens here. Will someone explain

> > this
> > > line of code (the middle one, not the if statement LOL) to me please?
> > > [hopefully I won't get flamed for this :-) ]
> > >
> > > Eric
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      13th Apr 2004
This calls the Dispose method of the connection (same thing the garbage
collector would call). In the case of SqlConnection, I believe both Dispose
and Close really do the same thing.

However, you should just use Close, as this is very clear in what it does.
This is sufficient in closing the connection and releasing it back into the
pool. The garbage collector will clean up the actual object when it runs.

"Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
news:%(E-Mail Removed)...
> In my Finally block, I was using cn.close (where cn is an ADO.NET
> connection object, SQLConnection to be exact) and then I came across the
> following in some microsoft code.
>
> If Not cn Is Nothing Then
> CType(cn, IDisposable).Dispose()
> End If
>
> I have to admit, I'm not sure what happens here. Will someone explain

this
> line of code (the middle one, not the if statement LOL) to me please?
> [hopefully I won't get flamed for this :-) ]
>
> Eric
>
>
>



 
Reply With Quote
 
Eric Sabine
Guest
Posts: n/a
 
      13th Apr 2004
Very helpful.


"Mike McIntyre [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Unmanaged resources should me managed by you. Learn more at:
>
> http://www.devcity.net/net/article.aspx?alias=gc_manage
>
> --
> Mike McIntyre
> Visual Basic MVP
> www.getdotnetcode.com
>
>
> "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
> news:(E-Mail Removed)...
> > Thank you. What you said makes sense. If I had used cn.close, I am

> simply
> > at the mercy of the garbage collector to clean it up when it wants.

Here
> > are 2 questions back to you.
> >
> > 1) is it not better to let the GC manage the resouces, or should I

> override
> > it?
> > 2) I notice that the SQLConnection object has its own Dispose method.

Why
> > do you suppose it's not used here?
> >
> > Eric
> >
> >
> >
> > "Raymond Lewallen" <(E-Mail Removed)> wrote in

message
> > news:%(E-Mail Removed)...
> > > Eric,
> > >
> > > The connection is converted to a type IDisposable and then the Dispose
> > > method is called to release unmanaged resources. When you call

> Dispose(),
> > > all the necessary cleanup is done when the call returns instead of

> waiting
> > > on the GC to do it when it feels it is necessary at an indeterminate

> point
> > > in the future.
> > >
> > > Raymond Lewallen
> > >
> > > "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
> > > news:#(E-Mail Removed)...
> > > > In my Finally block, I was using cn.close (where cn is an ADO.NET
> > > > connection object, SQLConnection to be exact) and then I came across

> the
> > > > following in some microsoft code.
> > > >
> > > > If Not cn Is Nothing Then
> > > > CType(cn, IDisposable).Dispose()
> > > > End If
> > > >
> > > > I have to admit, I'm not sure what happens here. Will someone

explain
> > > this
> > > > line of code (the middle one, not the if statement LOL) to me

please?
> > > > [hopefully I won't get flamed for this :-) ]
> > > >
> > > > Eric
> > > >
> > > >
> > > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
Al Reid
Guest
Posts: n/a
 
      13th Apr 2004
Just curious. If I close a connection, can I not, at a later time, just open it again and use it?

If I dispose of it, would I not have to reinitialize it before I could open it again?

If so, then Closing and Disposing are net the same.

--
Al Reid

"It ain't what you don't know that gets you into trouble. It's what you know
for sure that just ain't so." --- Mark Twain

"Marina" <(E-Mail Removed)> wrote in message news:%(E-Mail Removed)...
> This calls the Dispose method of the connection (same thing the garbage
> collector would call). In the case of SqlConnection, I believe both Dispose
> and Close really do the same thing.
>
> However, you should just use Close, as this is very clear in what it does.
> This is sufficient in closing the connection and releasing it back into the
> pool. The garbage collector will clean up the actual object when it runs.
>
> "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
> news:%(E-Mail Removed)...
> > In my Finally block, I was using cn.close (where cn is an ADO.NET
> > connection object, SQLConnection to be exact) and then I came across the
> > following in some microsoft code.
> >
> > If Not cn Is Nothing Then
> > CType(cn, IDisposable).Dispose()
> > End If
> >
> > I have to admit, I'm not sure what happens here. Will someone explain

> this
> > line of code (the middle one, not the if statement LOL) to me please?
> > [hopefully I won't get flamed for this :-) ]
> >
> > Eric
> >
> >
> >

>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      13th Apr 2004
Hi Marina,

In full respect of you and mostly we agree, however Angel has told that the
connection.dispose is one of the rare situations that has to be done
whenever and as soon as possible because of some behaviour of the
connection pooling.

And Angel is one of them who I always believe when it is about Ado.net.

Cor


 
Reply With Quote
 
Marina
Guest
Posts: n/a
 
      13th Apr 2004
I guess that really depends on what Dispose does. If all it does is close
the connection - then maybe you can. If it does more then that, then
possibly you wouldn't.

But closing the connection is suffient in releasing it back to the pool and
not taking up unnecessary resources.

"Al Reid" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Just curious. If I close a connection, can I not, at a later time, just

open it again and use it?
>
> If I dispose of it, would I not have to reinitialize it before I could

open it again?
>
> If so, then Closing and Disposing are net the same.
>
> --
> Al Reid
>
> "It ain't what you don't know that gets you into trouble. It's what you

know
> for sure that just ain't so." --- Mark Twain
>
> "Marina" <(E-Mail Removed)> wrote in message

news:%(E-Mail Removed)...
> > This calls the Dispose method of the connection (same thing the garbage
> > collector would call). In the case of SqlConnection, I believe both

Dispose
> > and Close really do the same thing.
> >
> > However, you should just use Close, as this is very clear in what it

does.
> > This is sufficient in closing the connection and releasing it back into

the
> > pool. The garbage collector will clean up the actual object when it

runs.
> >
> > "Eric Sabine" <mopar41@___ho_y_tmail.ScPoAmM> wrote in message
> > news:%(E-Mail Removed)...
> > > In my Finally block, I was using cn.close (where cn is an ADO.NET
> > > connection object, SQLConnection to be exact) and then I came across

the
> > > following in some microsoft code.
> > >
> > > If Not cn Is Nothing Then
> > > CType(cn, IDisposable).Dispose()
> > > End If
> > >
> > > I have to admit, I'm not sure what happens here. Will someone explain

> > this
> > > line of code (the middle one, not the if statement LOL) to me please?
> > > [hopefully I won't get flamed for this :-) ]
> > >
> > > Eric
> > >
> > >
> > >

> >
> >

>
>



 
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
Close a form two ways =?Utf-8?B?Um9iZXJ0X0xfUm9zcw==?= Microsoft Access Form Coding 2 3rd Aug 2005 07:11 PM
what are the ways to ensure a sql connection is close? =?Utf-8?B?QXNoYQ==?= Microsoft ASP .NET 8 27th Oct 2004 01:54 AM
How many ways to maintain an ISP connection (keep alive) -- 11 so far BillR Freeware 3 30th Aug 2004 03:05 PM
other ways to use/open a Database Connection? DraguVaso Microsoft ADO .NET 4 3rd Jun 2004 02:22 PM
other ways to use/open a Database Connection? DraguVaso Microsoft VB .NET 3 3rd Jun 2004 02:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:17 PM.