Vivek
Vivek

Reputation: 16508

Is is necessary to dispose DbCommand after use?

We use Enterprise Library 3.0 to access Oracle DB (microsoft oracle client). What happens when I do not dispose a DbCommand instance after a stored procedure or function is called? Does .NET automatically garbage collect them? Note that we do make sure that the transaction/connection gets closed and disposed properly.

Upvotes: 14

Views: 14163

Answers (4)

Mike Hofer
Mike Hofer

Reputation: 17022

From the documentation for IDisposable:

The primary use of this interface is to release unmanaged resources. The garbage collector automatically releases the memory allocated to a managed object when that object is no longer used. However, it is not possible to predict when garbage collection will occur. Furthermore, the garbage collector has no knowledge of unmanaged resources such as window handles, or open files and streams.

Use the Dispose method of this interface to explicitly release unmanaged resources in conjunction with the garbage collector. The consumer of an object can call this method when the object is no longer needed.

Given this, an object that implements IDisposable potentially maintains references to unmanaged resources. These resources are not released until the garbage collector comes along and collects the object. However, since you cannot know when the garbage collector will do this, disposable objects (such as OracleDbCommand) can hang around far longer than you might want them to.

If an object implements IDisposable, you should call it as soon as possible to release the unmanaged resources that it holds references to. This can be accomplished by either calling Dispose directly or by declaring it within a using block.

Upvotes: 5

John Saunders
John Saunders

Reputation: 161801

This is a duplicate, but I don't have time to find the original.

If it implements IDisposable, and if you created it, then you need to call Dispose on it. That's why the developer of the class made it implement IDisposable.

The garbage collector does not call Dispose on all IDisposable-implementing objects.

Upvotes: 20

Jeremy Frey
Jeremy Frey

Reputation: 2405

Reflector doesn't indicate that OracleCommand specifically overrides Dispose (from System.ComponentModel.Component's implementation, anyway), so chances are it won't hurt your application much if you don't call it.

The important thing, though, is that OracleCommand specifically implements IDbCommand, which specifically implements IDisposable. If you ever replaced your OracleCommand with another IDbCommand, then you would most likely want to use Dispose(). And while SqlCommand doesn't explicitly override Dispose(), Odbc and OleDb certainly do.

In short, since it's IDisposable, you should dispose it, just to be on the safe side.

Upvotes: 8

Matthew Groves
Matthew Groves

Reputation: 26169

Not 100% sure about Oracle, but when using SqlCommand, it must be disposed after use. You could either just call .Dispose(), or just put it in a using block, like so:


using(DbCommand cmd = new DbCommand(foo, bar))
{
     // use cmd object
}

Upvotes: 2

Related Questions