473,545 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Finalize and database connection... contradiction in msdn or a misunderstaning from my part ?



hi,

I have a component that uses a database connection.

In the finalizer I dispose the connection because I read in msdn the
following:

"A type must implement Finalize when it uses unmanaged resources such as
file handles or database connections that must be released when the
managed object that uses them is reclaimed."

but this resulted in the folowing exception:
Run-time exception thrown : System.InvalidO perationExcepti on - Handle is
not initialized.

Then while reading in msdn to see what might be causing this exception I
read the folowing :

"Do not call Close or Dispose on a Connection, a DataReader, or any
other managed object in the Finalize method of your class."

Is there a contradiction in msnd , or have I understood wrong ?
are handles file and database connections unmanaged ressources used in a
managed object or what ?

And another question:
I know that GC works when the more the memory is loaded. But is there a
memory limit for an application, or can it use all the PC memory ?

Regards

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
12 2099
No. There is no contradiction.

One statement is talking about UNMANAGED resources and the other is talking
about MANAGED objects.

I am sure there are a lot of subtleties about what ia a managed object and
what is an unmanaged resource, but the rule of thumb I apply is that if an
object is an instance of a class, etc. that is provided by or derived from
the framework then it is managed, otherwise it is unmanaged (e.g. a handle
or other resource obtained by calling an API function directly).
"Joe Abou Jaoude" <an*******@devd ex.com> wrote in message
news:uI******** ******@tk2msftn gp13.phx.gbl...


hi,

I have a component that uses a database connection.

In the finalizer I dispose the connection because I read in msdn the
following:

"A type must implement Finalize when it uses unmanaged resources such as
file handles or database connections that must be released when the
managed object that uses them is reclaimed."

but this resulted in the folowing exception:
Run-time exception thrown : System.InvalidO perationExcepti on - Handle is
not initialized.

Then while reading in msdn to see what might be causing this exception I
read the folowing :

"Do not call Close or Dispose on a Connection, a DataReader, or any
other managed object in the Finalize method of your class."

Is there a contradiction in msnd , or have I understood wrong ?
are handles file and database connections unmanaged ressources used in a
managed object or what ?

And another question:
I know that GC works when the more the memory is loaded. But is there a
memory limit for an application, or can it use all the PC memory ?

Regards

*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #2


hi stephany,
I agree with u that one statement is talking about UNMANAGED resources
and the other is talking about MANAGED objects....

But is the database connection a managed or unmanaged object ?
Basically, It should be considered as a managed object since it's an
instance of a class provided by the .Net framework.

However when reading this in msdn "A type must implement Finalize when
it uses unmanaged resources such as file handles or database connections
that must be released when the managed object that uses them is
reclaimed." I don't know what to think , because I don't think that a
database connection is obtained by calling an API
*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #3
You've answered your own question:

'considered as a managed object since it's an instance of a class provided
by the .Net framework'

If your database connection object uses any unmanaged resources then it
already knows about them and will implement Finalize to release them and you
dont not have to worry about this.

What it really means is that if you write your own class, 'Widget', that
obtains and holds an unmanaged resource then you are responsible for
releasing it by implementing Finalize.

Air Code:
---------
Class Widget

Private handle as IntPtr

Sub New()
' Obtain a handle to an unmanaged resource
GetHandle(handl e)
End Sub

Protected Sub Finalize()
' Release the unmanaged resource
CloseHandle(han dle)
End Sub

End Class

Get the picture?
"Joe Abou Jaoude" <an*******@devd ex.com> wrote in message
news:ub******** ******@tk2msftn gp13.phx.gbl...


hi stephany,
I agree with u that one statement is talking about UNMANAGED resources
and the other is talking about MANAGED objects....

But is the database connection a managed or unmanaged object ?
Basically, It should be considered as a managed object since it's an
instance of a class provided by the .Net framework.

However when reading this in msdn "A type must implement Finalize when
it uses unmanaged resources such as file handles or database connections
that must be released when the managed object that uses them is
reclaimed." I don't know what to think , because I don't think that a
database connection is obtained by calling an API
*** Sent via Developersdex http://www.developersdex.com ***

Nov 21 '05 #4
You are then saying that a graphics object created with the .CreateGraphics
method and the associated Pens and Brushes you use need not be disposed since
they will be disposed by the Finalize method of the class they are used in
which, from what I read, is correct but it is recommended that you dispose of
them as soon as you are finished with them and many on thie newsgroup also
advocate disposing of them rather than relying on the Finalize method.

A question, if you open a database (conn.open) then if you close it
(conn.close) you can reopen it at any time within the same scope using
conn.Open again. However, if you close it (conn.dispose), you can't reopen
it unless you re-instantiate it..is this correct?

"Stephany Young" wrote:
You've answered your own question:

'considered as a managed object since it's an instance of a class provided
by the .Net framework'

If your database connection object uses any unmanaged resources then it
already knows about them and will implement Finalize to release them and you
dont not have to worry about this.

What it really means is that if you write your own class, 'Widget', that
obtains and holds an unmanaged resource then you are responsible for
releasing it by implementing Finalize.

Air Code:
---------
Class Widget

Private handle as IntPtr

Sub New()
' Obtain a handle to an unmanaged resource
GetHandle(handl e)
End Sub

Protected Sub Finalize()
' Release the unmanaged resource
CloseHandle(han dle)
End Sub

End Class

Get the picture?
"Joe Abou Jaoude" <an*******@devd ex.com> wrote in message
news:ub******** ******@tk2msftn gp13.phx.gbl...


hi stephany,
I agree with u that one statement is talking about UNMANAGED resources
and the other is talking about MANAGED objects....

But is the database connection a managed or unmanaged object ?
Basically, It should be considered as a managed object since it's an
instance of a class provided by the .Net framework.

However when reading this in msdn "A type must implement Finalize when
it uses unmanaged resources such as file handles or database connections
that must be released when the managed object that uses them is
reclaimed." I don't know what to think , because I don't think that a
database connection is obtained by calling an API
*** Sent via Developersdex http://www.developersdex.com ***


Nov 21 '05 #5
In a nutshell, yes that is what I am saying. BUT ... it's a bit more
complicated than that.

Factors like, the scope of an object, how long the life of an object is, how
instantiated objects impact on available resources and probably a whole raft
of other things that I can't think of right now.

Take the following procedure as an example:

Private Sub AAA()

Dim _w as Widget

_w = New Widget

Me.Label1.Text = _w.Description

End Sub

All you are doing is instantiating a Widget object, grabbing the value of
its Description property and forgetting about it. Because the object
variable _w is local to the procedure it will 'go out of scope' as soon as
the Ens Sub is executed. When it goes out of scope it will be automatically
disposed of and, at that stage, will become avaible for the Garbage
Collector to have its wicked way with it when it is ready.

Now add a call to a long running procedure after you have grabbed the
Description and before the End Sub.

Private Sub AAA()

Dim _w as Widget

_w = New Widget

Me.Label1.Text = _w.Description

LongRunningProc edure()

End Sub

The Garbage Collector might do its stuff a number of times before
LongRunningProc edure returns, but _w will still not be tidied up until
sometime after LongRunningProc edure returns and the End Sub is executed. So,
while LongRunningProc edure is executing, _w is just sitting there doing
nothing but occupying some resource.

if you so wish, and it is not mandatory to do so, you might 'dispose' of _w
early. One way of doing this is to set it to nothing. Another way, if the
Widget class implements the IDisposable interface, is to call its Dispose
method. Setting it to nothing tidies it up properly even it it doesn't
implement IDisposable.

Private Sub AAA()

Dim _w as Widget

_w = New Widget

Me.Label1.Text = _w.Description

_w = Nothing

LongRunningProc edure()

End Sub

or

Private Sub AAA()

Dim _w as Widget

_w = New Widget

Me.Label1.Text = _w.Description

_w.Dispose()

LongRunningProc edure()

End Sub

Now, if the Garbage Collector does it suff (and feels so inclined) while
LongRunningProc edure is executing, then it might tidy up _w.

I have no intention of getting into a discussion of what the Garbage
Collecter does or how it does it's stuff.

In normal circumstances one does not really have to worry about it. You
really only have to worry about it if there is a specific reason to do so.

As you can see it is an complex area that, in my opinion is worthy of a book
specfically about Dispose, Finalize and the Garbage Collector.

All you can really do is read as much as you can on the subject, decide what
is good advice, what is bad advice and make your own judgement calls.

Remember that just because you read it somewhare does not make it fact. Some
people advocate explicit disposing of everting all the time, others advocate
never explicitly disposing. I take the middle ground and use it when I
consider it appropriate to do so.
"Dennis" <De****@discuss ions.microsoft. com> wrote in message
news:00******** *************** ***********@mic rosoft.com...
You are then saying that a graphics object created with the
.CreateGraphics
method and the associated Pens and Brushes you use need not be disposed
since
they will be disposed by the Finalize method of the class they are used in
which, from what I read, is correct but it is recommended that you dispose
of
them as soon as you are finished with them and many on thie newsgroup also
advocate disposing of them rather than relying on the Finalize method.

A question, if you open a database (conn.open) then if you close it
(conn.close) you can reopen it at any time within the same scope using
conn.Open again. However, if you close it (conn.dispose), you can't
reopen
it unless you re-instantiate it..is this correct?

"Stephany Young" wrote:
You've answered your own question:

'considered as a managed object since it's an instance of a class
provided
by the .Net framework'

If your database connection object uses any unmanaged resources then it
already knows about them and will implement Finalize to release them and
you
dont not have to worry about this.

What it really means is that if you write your own class, 'Widget', that
obtains and holds an unmanaged resource then you are responsible for
releasing it by implementing Finalize.

Air Code:
---------
Class Widget

Private handle as IntPtr

Sub New()
' Obtain a handle to an unmanaged resource
GetHandle(handl e)
End Sub

Protected Sub Finalize()
' Release the unmanaged resource
CloseHandle(han dle)
End Sub

End Class

Get the picture?
"Joe Abou Jaoude" <an*******@devd ex.com> wrote in message
news:ub******** ******@tk2msftn gp13.phx.gbl...
>
>
> hi stephany,
> I agree with u that one statement is talking about UNMANAGED resources
> and the other is talking about MANAGED objects....
>
> But is the database connection a managed or unmanaged object ?
> Basically, It should be considered as a managed object since it's an
> instance of a class provided by the .Net framework.
>
> However when reading this in msdn "A type must implement Finalize when
> it uses unmanaged resources such as file handles or database
> connections
> that must be released when the managed object that uses them is
> reclaimed." I don't know what to think , because I don't think that a
> database connection is obtained by calling an API
>
>
> *** Sent via Developersdex http://www.developersdex.com ***


Nov 21 '05 #6


ok, thank u for ur time

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #7
Dennis,

Not directly, did you try on a global created connection which is disposed
to add the connectionstrin g again. The dispose on the connection is
overloaded and removes the connectionstrin g.
Cor
Nov 21 '05 #8
Stephany,
| When it goes out of scope it will be automatically
| disposed of and, at that stage,
VB.NET 1.0 & 1.1 (VS.NET 2002 & 2003) does not "automatica lly dispose" of
anything! You need to manually call IDisposable.Dis pose if you want the
object to be disposed of.

VB.NET 2005 supports the Using statement (ala C#'s using statement in VS.NET
2002 & 2003) which will support "automatica lly disposing" of objects that
implement the IDisposable interface.

| The Garbage Collector might do its stuff a number of times before
| LongRunningProc edure returns, but _w will still not be tidied up until
| sometime after LongRunningProc edure returns and the End Sub is executed
This is true for Debug builds (I don't remember if you need to be also
running under the debugger or not), however under Release builds the GC &
JIT sees that _w is no longer used & makes it eligible for collection!
Setting a variable to Nothing, does exactly that, sets the variable to
Nothing. My understanding it may actually hurt the GC's job in release
builds. In release builds the JIT & GC can determine when the variable is no
longer being used & if you wait until after LongRunningProc edure is finished
to set it to Nothing, the GC & JIT may think the object is still in use,
possibly causing it (the object) to be promoted to the 1st or 2nd generation
heap.
Based on a couple of discussions last couple of weeks I have started a list
of when to Call Dispose, actually its now a list of when its OK not to call
Dispose! As you should always explicitly call Dispose if the framework does
not implicitly call it for you! Which happens to be my new rule for calling
IDisposable.Dis pose. However I've been busy & have not had a chance to
review the discussion publish a synopsis yet.
Some useful links on the GC:

http://msdn.microsoft.com/msdnmag/issues/1100/gci/
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/

http://msdn.microsoft.com/library/de...izeDispose.asp

Both of these blogs contain a plethora of articles on the GC:
http://blogs.msdn.com/ricom/
http://blogs.msdn.com/maoni/

Hope this helps
Jay
"Stephany Young" <noone@localhos t> wrote in message
news:uh******** ******@tk2msftn gp13.phx.gbl...
| In a nutshell, yes that is what I am saying. BUT ... it's a bit more
| complicated than that.
|
| Factors like, the scope of an object, how long the life of an object is,
how
| instantiated objects impact on available resources and probably a whole
raft
| of other things that I can't think of right now.
|
| Take the following procedure as an example:
|
| Private Sub AAA()
|
| Dim _w as Widget
|
| _w = New Widget
|
| Me.Label1.Text = _w.Description
|
| End Sub
|
| All you are doing is instantiating a Widget object, grabbing the value of
| its Description property and forgetting about it. Because the object
| variable _w is local to the procedure it will 'go out of scope' as soon as
| the Ens Sub is executed. When it goes out of scope it will be
automatically
| disposed of and, at that stage, will become avaible for the Garbage
| Collector to have its wicked way with it when it is ready.
|
| Now add a call to a long running procedure after you have grabbed the
| Description and before the End Sub.
|
| Private Sub AAA()
|
| Dim _w as Widget
|
| _w = New Widget
|
| Me.Label1.Text = _w.Description
|
| LongRunningProc edure()
|
| End Sub
|
| The Garbage Collector might do its stuff a number of times before
| LongRunningProc edure returns, but _w will still not be tidied up until
| sometime after LongRunningProc edure returns and the End Sub is executed.
So,
| while LongRunningProc edure is executing, _w is just sitting there doing
| nothing but occupying some resource.
|
| if you so wish, and it is not mandatory to do so, you might 'dispose' of
_w
| early. One way of doing this is to set it to nothing. Another way, if the
| Widget class implements the IDisposable interface, is to call its Dispose
| method. Setting it to nothing tidies it up properly even it it doesn't
| implement IDisposable.
|
| Private Sub AAA()
|
| Dim _w as Widget
|
| _w = New Widget
|
| Me.Label1.Text = _w.Description
|
| _w = Nothing
|
| LongRunningProc edure()
|
| End Sub
|
| or
|
| Private Sub AAA()
|
| Dim _w as Widget
|
| _w = New Widget
|
| Me.Label1.Text = _w.Description
|
| _w.Dispose()
|
| LongRunningProc edure()
|
| End Sub
|
| Now, if the Garbage Collector does it suff (and feels so inclined) while
| LongRunningProc edure is executing, then it might tidy up _w.
|
| I have no intention of getting into a discussion of what the Garbage
| Collecter does or how it does it's stuff.
|
| In normal circumstances one does not really have to worry about it. You
| really only have to worry about it if there is a specific reason to do so.
|
| As you can see it is an complex area that, in my opinion is worthy of a
book
| specfically about Dispose, Finalize and the Garbage Collector.
|
| All you can really do is read as much as you can on the subject, decide
what
| is good advice, what is bad advice and make your own judgement calls.
|
| Remember that just because you read it somewhare does not make it fact.
Some
| people advocate explicit disposing of everting all the time, others
advocate
| never explicitly disposing. I take the middle ground and use it when I
| consider it appropriate to do so.
|
|
| "Dennis" <De****@discuss ions.microsoft. com> wrote in message
| news:00******** *************** ***********@mic rosoft.com...
| > You are then saying that a graphics object created with the
| > .CreateGraphics
| > method and the associated Pens and Brushes you use need not be disposed
| > since
| > they will be disposed by the Finalize method of the class they are used
in
| > which, from what I read, is correct but it is recommended that you
dispose
| > of
| > them as soon as you are finished with them and many on thie newsgroup
also
| > advocate disposing of them rather than relying on the Finalize method.
| >
| > A question, if you open a database (conn.open) then if you close it
| > (conn.close) you can reopen it at any time within the same scope using
| > conn.Open again. However, if you close it (conn.dispose), you can't
| > reopen
| > it unless you re-instantiate it..is this correct?
| >
| > "Stephany Young" wrote:
| >
| >> You've answered your own question:
| >>
| >> 'considered as a managed object since it's an instance of a class
| >> provided
| >> by the .Net framework'
| >>
| >> If your database connection object uses any unmanaged resources then it
| >> already knows about them and will implement Finalize to release them
and
| >> you
| >> dont not have to worry about this.
| >>
| >> What it really means is that if you write your own class, 'Widget',
that
| >> obtains and holds an unmanaged resource then you are responsible for
| >> releasing it by implementing Finalize.
| >>
| >> Air Code:
| >> ---------
| >> Class Widget
| >>
| >> Private handle as IntPtr
| >>
| >> Sub New()
| >> ' Obtain a handle to an unmanaged resource
| >> GetHandle(handl e)
| >> End Sub
| >>
| >> Protected Sub Finalize()
| >> ' Release the unmanaged resource
| >> CloseHandle(han dle)
| >> End Sub
| >>
| >> End Class
| >>
| >> Get the picture?
| >>
| >>
| >> "Joe Abou Jaoude" <an*******@devd ex.com> wrote in message
| >> news:ub******** ******@tk2msftn gp13.phx.gbl...
| >> >
| >> >
| >> > hi stephany,
| >> > I agree with u that one statement is talking about UNMANAGED
resources
| >> > and the other is talking about MANAGED objects....
| >> >
| >> > But is the database connection a managed or unmanaged object ?
| >> > Basically, It should be considered as a managed object since it's an
| >> > instance of a class provided by the .Net framework.
| >> >
| >> > However when reading this in msdn "A type must implement Finalize
when
| >> > it uses unmanaged resources such as file handles or database
| >> > connections
| >> > that must be released when the managed object that uses them is
| >> > reclaimed." I don't know what to think , because I don't think that a
| >> > database connection is obtained by calling an API
| >> >
| >> >
| >> > *** Sent via Developersdex http://www.developersdex.com ***
| >>
| >>
| >>
|
|
Nov 21 '05 #9
OK. The sentence, from which you ommitted to quote the important bit, should
have read:

When it goes out of scope it will will become avaible for the Garbage
Collector to have its wicked way with it when it is ready.

Yes, I can see how the extraneous phrase could have been confusing if one
read the sentence, or that part of the sentence, out of context

The further point I was making was that the GC would do it's stuff at some
point in the function WITHOUT any special action from the programmer, and
that, in MOST CASES, one does not even need to consider it. I was not about
to attempt to list the cases where action from the programmer, but I am sure
that someone at MIT is probably writing a post-doctoral thesis on the
vagaries if the GC as we speak.
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:ux******** ******@TK2MSFTN GP12.phx.gbl...
Stephany,
| When it goes out of scope it will be automatically
| disposed of and, at that stage,
VB.NET 1.0 & 1.1 (VS.NET 2002 & 2003) does not "automatica lly dispose" of
anything! You need to manually call IDisposable.Dis pose if you want the
object to be disposed of.

VB.NET 2005 supports the Using statement (ala C#'s using statement in
VS.NET
2002 & 2003) which will support "automatica lly disposing" of objects that
implement the IDisposable interface.

| The Garbage Collector might do its stuff a number of times before
| LongRunningProc edure returns, but _w will still not be tidied up until
| sometime after LongRunningProc edure returns and the End Sub is executed
This is true for Debug builds (I don't remember if you need to be also
running under the debugger or not), however under Release builds the GC &
JIT sees that _w is no longer used & makes it eligible for collection!
Setting a variable to Nothing, does exactly that, sets the variable to
Nothing. My understanding it may actually hurt the GC's job in release
builds. In release builds the JIT & GC can determine when the variable is
no
longer being used & if you wait until after LongRunningProc edure is
finished
to set it to Nothing, the GC & JIT may think the object is still in use,
possibly causing it (the object) to be promoted to the 1st or 2nd
generation
heap.
Based on a couple of discussions last couple of weeks I have started a
list
of when to Call Dispose, actually its now a list of when its OK not to
call
Dispose! As you should always explicitly call Dispose if the framework
does
not implicitly call it for you! Which happens to be my new rule for
calling
IDisposable.Dis pose. However I've been busy & have not had a chance to
review the discussion publish a synopsis yet.
Some useful links on the GC:

http://msdn.microsoft.com/msdnmag/issues/1100/gci/
http://msdn.microsoft.com/msdnmag/issues/1200/GCI2/

http://msdn.microsoft.com/library/de...izeDispose.asp

Both of these blogs contain a plethora of articles on the GC:
http://blogs.msdn.com/ricom/
http://blogs.msdn.com/maoni/

Hope this helps
Jay
"Stephany Young" <noone@localhos t> wrote in message
news:uh******** ******@tk2msftn gp13.phx.gbl...
| In a nutshell, yes that is what I am saying. BUT ... it's a bit more
| complicated than that.
|
| Factors like, the scope of an object, how long the life of an object is,
how
| instantiated objects impact on available resources and probably a whole
raft
| of other things that I can't think of right now.
|
| Take the following procedure as an example:
|
| Private Sub AAA()
|
| Dim _w as Widget
|
| _w = New Widget
|
| Me.Label1.Text = _w.Description
|
| End Sub
|
| All you are doing is instantiating a Widget object, grabbing the value
of
| its Description property and forgetting about it. Because the object
| variable _w is local to the procedure it will 'go out of scope' as soon
as
| the Ens Sub is executed. When it goes out of scope it will be
automatically
| disposed of and, at that stage, will become avaible for the Garbage
| Collector to have its wicked way with it when it is ready.
|
| Now add a call to a long running procedure after you have grabbed the
| Description and before the End Sub.
|
| Private Sub AAA()
|
| Dim _w as Widget
|
| _w = New Widget
|
| Me.Label1.Text = _w.Description
|
| LongRunningProc edure()
|
| End Sub
|
| The Garbage Collector might do its stuff a number of times before
| LongRunningProc edure returns, but _w will still not be tidied up until
| sometime after LongRunningProc edure returns and the End Sub is executed.
So,
| while LongRunningProc edure is executing, _w is just sitting there doing
| nothing but occupying some resource.
|
| if you so wish, and it is not mandatory to do so, you might 'dispose' of
_w
| early. One way of doing this is to set it to nothing. Another way, if
the
| Widget class implements the IDisposable interface, is to call its
Dispose
| method. Setting it to nothing tidies it up properly even it it doesn't
| implement IDisposable.
|
| Private Sub AAA()
|
| Dim _w as Widget
|
| _w = New Widget
|
| Me.Label1.Text = _w.Description
|
| _w = Nothing
|
| LongRunningProc edure()
|
| End Sub
|
| or
|
| Private Sub AAA()
|
| Dim _w as Widget
|
| _w = New Widget
|
| Me.Label1.Text = _w.Description
|
| _w.Dispose()
|
| LongRunningProc edure()
|
| End Sub
|
| Now, if the Garbage Collector does it suff (and feels so inclined) while
| LongRunningProc edure is executing, then it might tidy up _w.
|
| I have no intention of getting into a discussion of what the Garbage
| Collecter does or how it does it's stuff.
|
| In normal circumstances one does not really have to worry about it. You
| really only have to worry about it if there is a specific reason to do
so.
|
| As you can see it is an complex area that, in my opinion is worthy of a
book
| specfically about Dispose, Finalize and the Garbage Collector.
|
| All you can really do is read as much as you can on the subject, decide
what
| is good advice, what is bad advice and make your own judgement calls.
|
| Remember that just because you read it somewhare does not make it fact.
Some
| people advocate explicit disposing of everting all the time, others
advocate
| never explicitly disposing. I take the middle ground and use it when I
| consider it appropriate to do so.
|
|
| "Dennis" <De****@discuss ions.microsoft. com> wrote in message
| news:00******** *************** ***********@mic rosoft.com...
| > You are then saying that a graphics object created with the
| > .CreateGraphics
| > method and the associated Pens and Brushes you use need not be
disposed
| > since
| > they will be disposed by the Finalize method of the class they are
used
in
| > which, from what I read, is correct but it is recommended that you
dispose
| > of
| > them as soon as you are finished with them and many on thie newsgroup
also
| > advocate disposing of them rather than relying on the Finalize method.
| >
| > A question, if you open a database (conn.open) then if you close it
| > (conn.close) you can reopen it at any time within the same scope using
| > conn.Open again. However, if you close it (conn.dispose), you can't
| > reopen
| > it unless you re-instantiate it..is this correct?
| >
| > "Stephany Young" wrote:
| >
| >> You've answered your own question:
| >>
| >> 'considered as a managed object since it's an instance of a class
| >> provided
| >> by the .Net framework'
| >>
| >> If your database connection object uses any unmanaged resources then
it
| >> already knows about them and will implement Finalize to release them
and
| >> you
| >> dont not have to worry about this.
| >>
| >> What it really means is that if you write your own class, 'Widget',
that
| >> obtains and holds an unmanaged resource then you are responsible for
| >> releasing it by implementing Finalize.
| >>
| >> Air Code:
| >> ---------
| >> Class Widget
| >>
| >> Private handle as IntPtr
| >>
| >> Sub New()
| >> ' Obtain a handle to an unmanaged resource
| >> GetHandle(handl e)
| >> End Sub
| >>
| >> Protected Sub Finalize()
| >> ' Release the unmanaged resource
| >> CloseHandle(han dle)
| >> End Sub
| >>
| >> End Class
| >>
| >> Get the picture?
| >>
| >>
| >> "Joe Abou Jaoude" <an*******@devd ex.com> wrote in message
| >> news:ub******** ******@tk2msftn gp13.phx.gbl...
| >> >
| >> >
| >> > hi stephany,
| >> > I agree with u that one statement is talking about UNMANAGED
resources
| >> > and the other is talking about MANAGED objects....
| >> >
| >> > But is the database connection a managed or unmanaged object ?
| >> > Basically, It should be considered as a managed object since it's
an
| >> > instance of a class provided by the .Net framework.
| >> >
| >> > However when reading this in msdn "A type must implement Finalize
when
| >> > it uses unmanaged resources such as file handles or database
| >> > connections
| >> > that must be released when the managed object that uses them is
| >> > reclaimed." I don't know what to think , because I don't think that
a
| >> > database connection is obtained by calling an API
| >> >
| >> >
| >> > *** Sent via Developersdex http://www.developersdex.com ***
| >>
| >>
| >>
|
|

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2472
by: R Reyes | last post by:
Hi, I'm trying to modularize my database connections a little better and get more out of my project with less code. First check out this common dbOpen() function inside class clsDatabase. I removed the try/catch part as it is not important for my question: // This function opens a connection to the database. public static SqlConnection...
8
1715
by: Jim | last post by:
I want to store a database connection (includes username & password) for my asp.net app, currently I have it stored in the web.config file - I know this is not ideal but can anyone suggest a better place or way to store it. Cheers Eath Worm Jim
2
3483
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info which each page will use, but my question relates to how to use the database connection i create in all my classes. I have a database class, in a...
7
2178
by: Bernie Yaeger | last post by:
I can't believe that there aren't lots of developers who: 1. create a crystal report that connects to sql server 2. calls the report using the crystalreportviewer control to view it and then, if appropriate, print it 3. tries to deploy that report to the client's database, but now it calls for a different database name Yet I can't get...
3
1497
by: DraguVaso | last post by:
Hi, In a VB.NET-application I use this to open a Database Connection: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim strConn As String strConn = "Server=BESQL1;DataBase=VocalcomCetelem;User ID=vocalcomcetelem;Password=vocalcomcetelem;Trusted_Connection=False"
1
350
by: Jim Mitchell | last post by:
I store my connect string in the viewstate and open and close the SQL database in every function or sub-routine call. Is there a better method of opening the database when the ASPX page opens and cleanly closing it when the page exits? I guess the on open part is easy, but how do I know that the page closes so that I can be sure to close out...
2
3815
by: Michel Esber | last post by:
Hello, DB2 V7 FP14 running Linux. Our application constantly tests whether the database connection is still alive before sending new statements to the DB. A "values current timestamp" statement is issued and, if there is no database exception, the connection acquired is considered to be alive. During one of my event monitors, this...
3
10280
by: Martin B | last post by:
Hallo! I'm working with C# .NET 2.0, implementing Client/Server Applications which are connecting via Network to SQL-Server or Oracle Databases. To stay independent from the underlaying Database I use System.Data.Common.DBConnection and .DBCommand. How can I keep aware from connection losses (network not availeable, db-server not...
5
1629
by: Sam | last post by:
Hi all, I have a process which first pulls one time all application IDs from a database and stores them in a table(this process works fine everytime). I then loop through the table, one at a time, and use application id to pull details info and process it. For example, if I have 500 records in my table, then I would have to open database...
0
7659
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7811
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7428
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5334
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3455
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1019
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.