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.Open();
}
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.close() 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(SqlConnection connection = new SqlConnection())
> > {
> > ......
> > }
> >
> > or
> >
> >
> > SqlConnection connection = null;
> >
> > try
> > {
> > connection = new SqlConnection();
> > ....
> > }
> > finally
> > {
> > if(connection != null)
> > {
> > connection.Close();
> > }
> > }
> >
> >
> > > 3) When i m saying connection.close(), 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 connectionstring 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.close() 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.close(), 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