473,545 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Database Connection questions.

Hi all,

I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.

1) If I have opened a connection to a database through Connection.open ()
method, and I do not use Connection.clos e() method, will garbage collector
collect the connection object just because i am not using it any more.

2) I am using a dataset, in which i make some modifications to the data and
submit the modified data back to the server. Will the connection be opened
again to the database server, because dataset is a connectionless object.

3) When i m saying connection.clos e(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open () method to call it.

thanks in advance
pradeep T.P
Nov 17 '05 #1
11 2407
Pradeep.
I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.

1) If I have opened a connection to a database through Connection.open ()
method, and I do not use Connection.clos e() method, will garbage collector
collect the connection object just because i am not using it any more.
The connection object will live as long as there is a reference to or from
it. See later answers as well about this.

2) I am using a dataset, in which i make some modifications to the data
and
submit the modified data back to the server. Will the connection be opened
again to the database server, because dataset is a connectionless object.
The dataset cannot sent to a server. The dataadapter is sending individual
rows to the server. One of the operations from a dataadapter is to open and
close the connection that is in his connection properties if that is/was
not already open when it started.
3) When i m saying connection.clos e(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open () method to call it.


The connection closes and when the connection object is declared globaly (or
by instance a dataadapter which has a reference to it) than the object
stays in memory.

By the way, this are typical questions for the newsgroup.

Microsoft.publi c.dotnet.framew ork.adonet

I hope this helps,

Cor

Nov 17 '05 #2
> 1) If I have opened a connection to a database through Connection.open ()
method, and I do not use Connection.clos e() method, will garbage collector
collect the connection object just because i am not using it any more.
The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. Also the GC is
non-deterministic so the connection may lie open for a long time before the
GC runs. It is best to put your connection useage inside a using statement
that will automatically call dispose automatically (which internally calls
close) or always use a finally statement i.e.

using(SqlConnec tion connection = new SqlConnection() )
{
.......
}

or
SqlConnection connection = null;

try
{
connection = new SqlConnection() ;
....
}
finally
{
if(connection != null)
{
connection.Clos e();
}
}

3) When i m saying connection.clos e(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open () method to call it.
When you call Close on a connection or Dispose the underlying connection is
returned to the connection pool, the connection pool group connections
together based on the Connection String, so the next time you try to open a
connection with the same connectionstrin g you will get a connection in the
pool (if there is one available) very quickly without a performance hit.
Mark R Dawson
http://www.markdawson.org

"pradeep_TP " wrote:
Hi all,

I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.

1) If I have opened a connection to a database through Connection.open ()
method, and I do not use Connection.clos e() method, will garbage collector
collect the connection object just because i am not using it any more.

2) I am using a dataset, in which i make some modifications to the data and
submit the modified data back to the server. Will the connection be opened
again to the database server, because dataset is a connectionless object.

3) When i m saying connection.clos e(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open () method to call it.

thanks in advance
pradeep T.P

Nov 17 '05 #3
The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. ...


Does "references " mean opened connection object remaining unclosed. What i
mean is that if after i open the connection object and does not close it for
long, will the GC consider this as unreferenced and try to collect the
object.

"Mark R. Dawson" wrote:
1) If I have opened a connection to a database through Connection.open ()
method, and I do not use Connection.clos e() method, will garbage collector
collect the connection object just because i am not using it any more.


The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. Also the GC is
non-deterministic so the connection may lie open for a long time before the
GC runs. It is best to put your connection useage inside a using statement
that will automatically call dispose automatically (which internally calls
close) or always use a finally statement i.e.

using(SqlConnec tion connection = new SqlConnection() )
{
......
}

or
SqlConnection connection = null;

try
{
connection = new SqlConnection() ;
....
}
finally
{
if(connection != null)
{
connection.Clos e();
}
}

3) When i m saying connection.clos e(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open () method to call it.


When you call Close on a connection or Dispose the underlying connection is
returned to the connection pool, the connection pool group connections
together based on the Connection String, so the next time you try to open a
connection with the same connectionstrin g you will get a connection in the
pool (if there is one available) very quickly without a performance hit.
Mark R Dawson
http://www.markdawson.org

"pradeep_TP " wrote:
Hi all,

I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.

1) If I have opened a connection to a database through Connection.open ()
method, and I do not use Connection.clos e() method, will garbage collector
collect the connection object just because i am not using it any more.

2) I am using a dataset, in which i make some modifications to the data and
submit the modified data back to the server. Will the connection be opened
again to the database server, because dataset is a connectionless object.

3) When i m saying connection.clos e(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open () method to call it.

thanks in advance
pradeep T.P

Nov 17 '05 #4

"pradeep_TP " <pr*******@disc ussions.microso ft.com> wrote in message
news:B0******** *************** ***********@mic rosoft.com...
The garbage collector will only do this if you do not have any references to the connection anywhere else in your program. ...
Does "references " mean opened connection object remaining unclosed. What

i mean is that if after i open the connection object and does not close it for long, will the GC consider this as unreferenced and try to collect the
object.


Do you mean like this:

public void LeakConnection( )
{
SqlConnection _connection = new SqlConnection(" some_connection _string");
_connection.Ope n();
}

I believe that when "_connectio n" goes out of scope it is eligible for
garbage collection. The garbage collector automatically calls "Dispose()" on
the "_connectio n" object which will close the connection.

So the answer is yes, it will *eventually* get closed automatically. But it
would be much, much, much better to either close it yourself or dispose
"_connectio n" yourself. Someone correct me if I'm wrong here. :)
Nov 17 '05 #5
One point of clarification, the Gabage Collector will not call Dispose
directly, it does not know anything about the IDisposable interface. If you
want Dispose to be called by the GC then you have to make sure you call
Dispose in a Finalizer in the class which is what gets called by the GC when
the object is elligable for cleanup.

"Scott Roberts" wrote:

"pradeep_TP " <pr*******@disc ussions.microso ft.com> wrote in message
news:B0******** *************** ***********@mic rosoft.com...
The garbage collector will only do this if you do not have any references to the connection anywhere else in your program. ...


Does "references " mean opened connection object remaining unclosed. What

i
mean is that if after i open the connection object and does not close it

for
long, will the GC consider this as unreferenced and try to collect the
object.


Do you mean like this:

public void LeakConnection( )
{
SqlConnection _connection = new SqlConnection(" some_connection _string");
_connection.Ope n();
}

I believe that when "_connectio n" goes out of scope it is eligible for
garbage collection. The garbage collector automatically calls "Dispose()" on
the "_connectio n" object which will close the connection.

So the answer is yes, it will *eventually* get closed automatically. But it
would be much, much, much better to either close it yourself or dispose
"_connectio n" yourself. Someone correct me if I'm wrong here. :)

Nov 17 '05 #6
What I meant was that an object is only elligable for GC once there is
nothing referencing it i.e. you don't have any variables pointing to the
object, for example if you have:
class MyObject
{
private SqlConnection _connection = null;

public MyObject()
{
_connection = new SqlConnection(" connection string");
_connection.Ope n();
}

public void DoSomething()
{
_connection = null;
}
}
The only way the connection object can become eligable for colllection by
the GC is for someone to call DoSomething() which breaks the link between the
_connection variable and the SqlConnection object. Now the SQL connection
object has nothing pointing to it, there is no way you can ever reference the
object again, it has effectively gone out of scope.

Or the instance of MyObject that gets created is no longer pointed to by any
variables then by transitive association ie a->b->c => a->c the _connection
object also has no way of being accessed.

Remember though that the GC is undeterministic and may not run for a long
time so your connection may remain open for a long time, therefore always
best to be very careful with closing connections.
Mark R Dawson
http://www.markdawson.org
"pradeep_TP " wrote:
The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. ...


Does "references " mean opened connection object remaining unclosed. What i
mean is that if after i open the connection object and does not close it for
long, will the GC consider this as unreferenced and try to collect the
object.

"Mark R. Dawson" wrote:
1) If I have opened a connection to a database through Connection.open ()
method, and I do not use Connection.clos e() method, will garbage collector
collect the connection object just because i am not using it any more.


The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. Also the GC is
non-deterministic so the connection may lie open for a long time before the
GC runs. It is best to put your connection useage inside a using statement
that will automatically call dispose automatically (which internally calls
close) or always use a finally statement i.e.

using(SqlConnec tion connection = new SqlConnection() )
{
......
}

or
SqlConnection connection = null;

try
{
connection = new SqlConnection() ;
....
}
finally
{
if(connection != null)
{
connection.Clos e();
}
}

3) When i m saying connection.clos e(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open () method to call it.


When you call Close on a connection or Dispose the underlying connection is
returned to the connection pool, the connection pool group connections
together based on the Connection String, so the next time you try to open a
connection with the same connectionstrin g you will get a connection in the
pool (if there is one available) very quickly without a performance hit.
Mark R Dawson
http://www.markdawson.org

"pradeep_TP " wrote:
Hi all,

I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.

1) If I have opened a connection to a database through Connection.open ()
method, and I do not use Connection.clos e() method, will garbage collector
collect the connection object just because i am not using it any more.

2) I am using a dataset, in which i make some modifications to the data and
submit the modified data back to the server. Will the connection be opened
again to the database server, because dataset is a connectionless object.

3) When i m saying connection.clos e(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open () method to call it.

thanks in advance
pradeep T.P

Nov 17 '05 #7

"Mark R. Dawson" <Ma*********@di scussions.micro soft.com> wrote in message
news:19******** *************** ***********@mic rosoft.com...
One point of clarification, the Gabage Collector will not call Dispose
directly, it does not know anything about the IDisposable interface. If you want Dispose to be called by the GC then you have to make sure you call
Dispose in a Finalizer in the class which is what gets called by the GC when the object is elligable for cleanup.


Ah, yes. Thank you.
Nov 17 '05 #8
I now understood what mark said. I would also like to know when connection
object is collected by GC, will the "connection object" be given back to the
connection pool. The other day i went throug the MSDN site, there I got an
explanation about connection pooling. I said that, once the connection is
closed, the "connection object" is given back to the connection pool. so if
GC collects the unclosed connection, will the connection object be still
returned back to connection pool

thanks all for your help :)
"Mark R. Dawson" wrote:
What I meant was that an object is only elligable for GC once there is
nothing referencing it i.e. you don't have any variables pointing to the
object, for example if you have:
class MyObject
{
private SqlConnection _connection = null;

public MyObject()
{
_connection = new SqlConnection(" connection string");
_connection.Ope n();
}

public void DoSomething()
{
_connection = null;
}
}
The only way the connection object can become eligable for colllection by
the GC is for someone to call DoSomething() which breaks the link between the
_connection variable and the SqlConnection object. Now the SQL connection
object has nothing pointing to it, there is no way you can ever reference the
object again, it has effectively gone out of scope.

Or the instance of MyObject that gets created is no longer pointed to by any
variables then by transitive association ie a->b->c => a->c the _connection
object also has no way of being accessed.

Remember though that the GC is undeterministic and may not run for a long
time so your connection may remain open for a long time, therefore always
best to be very careful with closing connections.
Mark R Dawson
http://www.markdawson.org
"pradeep_TP " wrote:
The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. ...


Does "references " mean opened connection object remaining unclosed. What i
mean is that if after i open the connection object and does not close it for
long, will the GC consider this as unreferenced and try to collect the
object.

"Mark R. Dawson" wrote:
> 1) If I have opened a connection to a database through Connection.open ()
> method, and I do not use Connection.clos e() method, will garbage collector
> collect the connection object just because i am not using it any more.

The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. Also the GC is
non-deterministic so the connection may lie open for a long time before the
GC runs. It is best to put your connection useage inside a using statement
that will automatically call dispose automatically (which internally calls
close) or always use a finally statement i.e.

using(SqlConnec tion connection = new SqlConnection() )
{
......
}

or
SqlConnection connection = null;

try
{
connection = new SqlConnection() ;
....
}
finally
{
if(connection != null)
{
connection.Clos e();
}
}
> 3) When i m saying connection.clos e(), is the connection actually getting
> close or does the connection information stays in some memory area, only
> waiting for some other connection.open () method to call it.

When you call Close on a connection or Dispose the underlying connection is
returned to the connection pool, the connection pool group connections
together based on the Connection String, so the next time you try to open a
connection with the same connectionstrin g you will get a connection in the
pool (if there is one available) very quickly without a performance hit.
Mark R Dawson
http://www.markdawson.org

"pradeep_TP " wrote:

> Hi all,
>
> I have a few questions that I have been wanting to ask for long. These are
> all related to ADO.net and specifically to conenction to database.
>
> 1) If I have opened a connection to a database through Connection.open ()
> method, and I do not use Connection.clos e() method, will garbage collector
> collect the connection object just because i am not using it any more.
>
> 2) I am using a dataset, in which i make some modifications to the data and
> submit the modified data back to the server. Will the connection be opened
> again to the database server, because dataset is a connectionless object.
>
> 3) When i m saying connection.clos e(), is the connection actually getting
> close or does the connection information stays in some memory area, only
> waiting for some other connection.open () method to call it.
>
> thanks in advance
> pradeep T.P

Nov 17 '05 #9
Hi pradeep,
the GC will call the SQLConnections Finalizer which in turn calls Dispose
which will then internally close the connection sending it back to the
connection pool.

"pradeep_TP " wrote:
I now understood what mark said. I would also like to know when connection
object is collected by GC, will the "connection object" be given back to the
connection pool. The other day i went throug the MSDN site, there I got an
explanation about connection pooling. I said that, once the connection is
closed, the "connection object" is given back to the connection pool. so if
GC collects the unclosed connection, will the connection object be still
returned back to connection pool

thanks all for your help :)
"Mark R. Dawson" wrote:
What I meant was that an object is only elligable for GC once there is
nothing referencing it i.e. you don't have any variables pointing to the
object, for example if you have:
class MyObject
{
private SqlConnection _connection = null;

public MyObject()
{
_connection = new SqlConnection(" connection string");
_connection.Ope n();
}

public void DoSomething()
{
_connection = null;
}
}
The only way the connection object can become eligable for colllection by
the GC is for someone to call DoSomething() which breaks the link between the
_connection variable and the SqlConnection object. Now the SQL connection
object has nothing pointing to it, there is no way you can ever reference the
object again, it has effectively gone out of scope.

Or the instance of MyObject that gets created is no longer pointed to by any
variables then by transitive association ie a->b->c => a->c the _connection
object also has no way of being accessed.

Remember though that the GC is undeterministic and may not run for a long
time so your connection may remain open for a long time, therefore always
best to be very careful with closing connections.
Mark R Dawson
http://www.markdawson.org
"pradeep_TP " wrote:

> The garbage collector will only do this if you do not have any references to
> the connection anywhere else in your program. ...

Does "references " mean opened connection object remaining unclosed. What i
mean is that if after i open the connection object and does not close it for
long, will the GC consider this as unreferenced and try to collect the
object.

"Mark R. Dawson" wrote:

> > 1) If I have opened a connection to a database through Connection.open ()
> > method, and I do not use Connection.clos e() method, will garbage collector
> > collect the connection object just because i am not using it any more.
>
> The garbage collector will only do this if you do not have any references to
> the connection anywhere else in your program. Also the GC is
> non-deterministic so the connection may lie open for a long time before the
> GC runs. It is best to put your connection useage inside a using statement
> that will automatically call dispose automatically (which internally calls
> close) or always use a finally statement i.e.
>
> using(SqlConnec tion connection = new SqlConnection() )
> {
> ......
> }
>
> or
>
>
> SqlConnection connection = null;
>
> try
> {
> connection = new SqlConnection() ;
> ....
> }
> finally
> {
> if(connection != null)
> {
> connection.Clos e();
> }
> }
>
>
> > 3) When i m saying connection.clos e(), is the connection actually getting
> > close or does the connection information stays in some memory area, only
> > waiting for some other connection.open () method to call it.
>
> When you call Close on a connection or Dispose the underlying connection is
> returned to the connection pool, the connection pool group connections
> together based on the Connection String, so the next time you try to open a
> connection with the same connectionstrin g you will get a connection in the
> pool (if there is one available) very quickly without a performance hit.
>
>
> Mark R Dawson
> http://www.markdawson.org
>
>
>
> "pradeep_TP " wrote:
>
> > Hi all,
> >
> > I have a few questions that I have been wanting to ask for long. These are
> > all related to ADO.net and specifically to conenction to database.
> >
> > 1) If I have opened a connection to a database through Connection.open ()
> > method, and I do not use Connection.clos e() method, will garbage collector
> > collect the connection object just because i am not using it any more.
> >
> > 2) I am using a dataset, in which i make some modifications to the data and
> > submit the modified data back to the server. Will the connection be opened
> > again to the database server, because dataset is a connectionless object.
> >
> > 3) When i m saying connection.clos e(), is the connection actually getting
> > close or does the connection information stays in some memory area, only
> > waiting for some other connection.open () method to call it.
> >
> > thanks in advance
> > pradeep T.P

Nov 17 '05 #10

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

Similar topics

0
1001
by: Nico Raddatz | last post by:
Hi all. I'm migrating a small app from vb6 to vb.net. It basically deals with invoices. This is the first time I do this kind of migration, from an existing app to a new one, the new app should be as "object oriented" as practically possible. I have several objects that are dependent on a database connection, such as the Invoice, the...
4
3403
by: Ish Ahluwalia | last post by:
Hi: I'm very new to PostGreSql database and actually looking to evaluate for one of our project needs. Below please find some questions. I'm hoping if I can get some insight into these questions. Thanks. Ish...
4
2458
by: Macca | last post by:
Hi, I have an windows forms application that accesses a SQL database I have a few questions as to connecting to the database. This application will run 24 hours a day. It is a monitoring application and will store events that happen in the database (These events happen randomly without pattern, between 10-50 a day) . There are a...
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...
5
2917
by: Roy Gourgi | last post by:
Hi, I am used to working in Visual FoxPro and I would like to be able to create a database and store and retrieve information from it. What is the simplest way to do it and what should I be using as there are many choices to choose from. My database will contain a lot of records. TIA
4
4877
by: Digital Fart | last post by:
howto make a connection to database available in my classes. What is the best practice when i want to write classes that need a connection to the database? Do i make a conn variable in my main() and give it as a parameter to every object i make that needs access to the database ex.
18
9108
by: surfrat_ | last post by:
Hi, I am having the following problems in getting Microsoft Visual Studio 2005 Professional to link to an Access .mdb database. Please help me to sort this out. Problem 1: The Microsoft page "How to: Connect to Data in an Access Database"
4
1473
by: twave | last post by:
I have a form with a list of yes/no questions which records to an Access database. Occasionally a string of 'yes' answers are being recorded as 'no' in the database. For example, the first 3 may record as 'yes' then the remaining questions all record as 'no'. I have set up tests to check the SQL statements which confirm the data being sent is...
0
4736
MrMancunian
by: MrMancunian | last post by:
How to create a database connection without using wizards Introduction I've seen a lot of questions on the net about getting data from, and saving data to databases. Here's a little insight how to do that using .NET code. I prefer using code instead of wizards, because you are completely in charge of connections. This article is written for...
0
7467
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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
7807
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...
0
7756
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
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
3450
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
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.