when to explicitly close/release objects/resources

D

djc

what objects do I need to be sure to explicitly close calling
object.close()? datasets? datatables? connections? dataviews? datareaders?
and how should it be done? .close()? .dispose()? set=Nothing?

I am new to asp.net/ado.net/vb.net but I used to always explicitly close
things like recordsets in old access dao programming... I need to know
how/when to do this with asp/net/ado.net/vb.net.

any info would be greatly appreciated. Thanks.
 
S

Scott M.

djc said:
what objects do I need to be sure to explicitly close calling
object.close()?

Pretty much anything you had to call .Open() on in order to use it in the
first place.

datasets? No
datatables? No
connections? Yes!
dataviews? No
datareaders? Yes.
and how should it be done?

.close()? Sure
.dispose()? If the object uses unmanaged resources (like databases, files,
other unmanged code, etc.)
set=Nothing? Although there are *some* places where this can help, by and
large, you don't need to do this anymore.
 
G

Guest

If you are using C# you have the option to use the using statement wich will
call Dispose() at the end of the scope.
e.g:
using( OleDbConnection connection = new OleDbConnection( conn ) )
{
using( OleDbCommand command = connection.CreateCommand() )
{
...
}
}
 
D

djc

Thank you!

Scott M. said:
Pretty much anything you had to call .Open() on in order to use it in the
first place.

datasets? No
datatables? No
connections? Yes!
dataviews? No
datareaders? Yes.


.close()? Sure
.dispose()? If the object uses unmanaged resources (like databases, files,
other unmanged code, etc.)
set=Nothing? Although there are *some* places where this can help, by and
large, you don't need to do this anymore.
 
D

djc

thanks for the info.

Tor Martin Halvorsen said:
If you are using C# you have the option to use the using statement wich will
call Dispose() at the end of the scope.
e.g:
using( OleDbConnection connection = new OleDbConnection( conn ) )
{
using( OleDbCommand command = connection.CreateCommand() )
{
...
}
}
 
D

djc

I forgot to include this: what about using .dispose(), set=nothing, etc...
(are there others?) on object variables like:
-commands?
-arrays?
-collections?
-hashtables?
 
V

Vladimir Sergeyev [MS]

This is actually the best option, since it guarantees that the object will
be disposed of even if there is an exception inside "using" block. That is

using(OleDbConnection ...) {
}

is similar to

OleDbConnection conn = null;
try {
conn = new OleDbConnection(...);
....
}
finally {
if (conn != null)
((IDisposable)conn).Dispose();
}

Cheers,
V.
 
S

Scott M.

Again, setting objects = nothing is not required in .NET (I know that takes
some getting used to if you are coming from a VB 6 world, but it's true).

And, as I mentioned in the earlier post, you only need to call dispose on
objects that use unmanaged resources (things the .NET Common Language
Runtime is not responsible for), so:


commands? Not generally
arrays? No (although you may need to loop through the array and dispose the
ojbects stored in the array depending on what those objects are.)
collections? Same as array
hashtables? Same as array
 
G

Guest

I need answer of following questions We are using SQL Server...

1. What happens when I set null to an object. Is GC loses its references
including other objects within that object?

2. Is SqlProvider uses connection pooling? If yes how can I turn off it? or
it is possible?

3. I had to continue a mcsm project. I saw that mcsm uses db connection
frequently. We have some additional modules. Each module uses its own DB and
site traffic is very high. Thus aspnet_wp reaches maximum memory limit easily
and uses %100 of CPU. Under that conditions should I compile those modules
under one db and one db work dll? Also do u suggests that connection pooling
will be off?

Thanks...
 

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