Connection question

  • Thread starter Thread starter Miguel
  • Start date Start date
M

Miguel

Hey, hi all


It's said framework's garbage collector destroy all
objects in applications or whatever, so my cuestion is:

if i have a Database connection object and if i dont close
this connection, when the garbage colletor destroy this
object it is closing the connection????


And how uneffcicient is it???, i mean if i dont close my
connection and i let this task for garbage collector (in
the case that it is possible)
 
If you're using a DataAdapter, you don't need to
explicitly open a connection in the first place. So,
don't worry about closing it. My understanding is that
the DataAdapter uses pooled connections, which may or may
not be closed right away, depending on whether other
processes need connections, etc. (Very much a factor in
three-tier Web Apps.)

If your using a DataReader, you're using a dedicated
connection that should not be held open. You won't be
able to re-use it for anything else anyway. Close the
damn thing.

So, those are the basic guidelines - is there any special
reason you have for doing anything different?
 
Hi Miguel,

It is very inneficient to leave connection open.
You should close it ASAP and open it just before db operations.
There is no performance hit since ado.net uses connection pooling.
 
Not really, i'm using multithreading,and for each thread
my program creates, it creates a database connection, so i
was a little afraid of using multithrading 'cos i new in
dotnet :)

Is correct create a database connection for each thread???
or is there a better and more efficient way????

Regards
 
I'm fairly sure that the same principles apply: If you're
using a Data Adapter, a new connection may or may not be
physically created when one of your threads instantiates a
DataConnection object. Connection pooling should cause an
existing connection to be re-used, if that is the optimum
thing to do.

If your using a DataReader, the Connection is dedicated to
the individual Reader. Open it, get the data you need,
close it. I could be wrong but I don't think that sharing
such a connection with other Readers is an option anyway.

So, I don't think the fact that your app uses multiple
threads changes things much.

HTH
 
It is not only correct but completely necessary to create a database
connection in each thread. Open the connection as late as possible, close it
or dispose it as soon as possible on a finally block (or rely on the "using"
construct in c#). This design maximizes the built in connection pooler in
the silliest provider you will be pleasantly surprised at how easy to code,
problem free, performant and scalable your code will be.

Using this design I have run five thousand concurrent threads, I actually
found that the code runs better by reducing the default max pool size to 10
from 100 and even with very heavy use I never run out of connections,
pooling is really very efficient.

Hope this helps
 
That's what happens when you rely on spell checkers
"This design maximizes the built in connection pooler in the silliest
provider "
was meant to say
"This design maximizes the built in connection pooler in the SqlClient
provider"

Not a Freudian slip I swear! :)
--
Angel Saenz-Badillos [MS] Managed Providers
This posting is provided "AS IS", with no warranties, and confers no
rights.Please do not send email directly to this alias.
This alias is for newsgroup purposes only.

Angel Saenz-Badillos said:
It is not only correct but completely necessary to create a database
connection in each thread. Open the connection as late as possible, close it
or dispose it as soon as possible on a finally block (or rely on the "using"
construct in c#). This design maximizes the built in connection pooler in
the silliest provider you will be pleasantly surprised at how easy to code,
problem free, performant and scalable your code will be.

Using this design I have run five thousand concurrent threads, I actually
found that the code runs better by reducing the default max pool size to 10
from 100 and even with very heavy use I never run out of connections,
pooling is really very efficient.

Hope this helps
--
Angel Saenz-Badillos [MS] Managed Providers
This posting is provided "AS IS", with no warranties, and confers no
rights.Please do not send email directly to this alias.
This alias is for newsgroup purposes only.

Not really, i'm using multithreading,and for each thread
my program creates, it creates a database connection, so i
was a little afraid of using multithrading 'cos i new in
dotnet :)

Is correct create a database connection for each thread???
or is there a better and more efficient way????

Regards
 
Back
Top