Connection.Close

  • Thread starter Daniel Bello Urizarri
  • Start date
D

Daniel Bello Urizarri

Hello:

I'n havig problems with an exception generated on a finalize method while
trying to Close(); a OleDbConnection.

While in the Close method documentation can be readed:

"CAUTION Do not call Close or Dispose on a Connection, a DataReader, or
any other managed object in the Finalize method of your class. In a
finalizer, you should only release unmanaged resources that your class owns
directly. If your class does not own any unmanaged resources, do not include
a Finalize method in your class definition. For more information, see
Programming for Garbage Collection."

The "about OleDbConnection class" documentation says:

If the OleDbConnection goes out of scope, it is not closed. Therefore, you
must explicitly close the connection by calling Close or Dispose.

Is the destruction of an object instance an "out of scope" situation?
What to to do? Shoudl I see "Programming for Garbage Collection"?
 
F

Frank Oquendo

Daniel said:
Is the destruction of an object instance an "out of scope" situation?
What to to do? Shoudl I see "Programming for Garbage Collection"?

Why not call Close or Dispose yourself? Or wrap it in a using block.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
D

Daniel Bello Urizarri

I want to call Close, but i don't know when. When the of my object
destructor is invoked an exception is thrown, and the connection object
should exist while the object exists..

~MainObject ()
{
if ( Connection != null && Connection.State == ConnectionState.Open )
Connection.Close (); //INVALID HANDLE EXCEPTION
}
 
F

Frank Oquendo

Daniel said:
I want to call Close, but i don't know when.

In the same procedure where you opened the conenction, as soon as you've
retrieved whatever data you're after:

OleDbConnection dbConnection = new OleDbConnection(connString);
// yada, yada, yada. Now we're done with the connection
dbConnection.Close();
When the of my object
destructor is invoked an exception is thrown, and the connection
object should exist while the object exists..

As you've already noted, we've been warned not to try and dispose of
managed resources in our finalizer methods.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
D

Daniel Bello Urizarri

That works, but is more slow that keeping the connection open and closing it
when it will not be needed again.
 
F

Frank Oquendo

Daniel said:
That works, but is more slow that keeping the connection open and
closing it when it will not be needed again.

ADO.NET is specifically designed to work with disconnected data and
performs connection pooling transparently. Keeping your connections open
will hamper performance.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
M

Miha Markic

In addition to Frank - by default ado.net will keep minimum one connection
to database open - even if you close all connections explicitly.
So, there is really no need to keep them open.
 

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