Does closing a database connection in Dispose method is right?

Dispose is never called automatically. The connection will not be closed until the Dispose method of your object is explicitly called, or if your class in used in a using() block A safer way is to call the dispose method in your finalizer and ensure the finalizer is suppressed when the Dispose method is called. This article present the correct way to implement the pattern Hope it helps!

Cédric.

– agnieszka Jun 19 '09 at 8:24 When your application is shut down, all finalizers should be called by the garbage collector, so at least the connections will be closed as the finalizer of the Connection objects will be triggered... but that's a lame way to close a connection ;o) – Cédric Rup Jun 19 '09 at 8:29 1 And by the way : Dispose method are never called automatically. In your current state of code, connections are closed because the dispose method of Connection is called in their finalizers – Cédric Rup Jun 19 '09 at 8:31 I know, it's not my code as I already said ;) – agnieszka Jun 19 '09 at 8:32 do you mean that this method will be never called anyway? – agnieszka Jun 19 '09 at 8:33.

Conn.Dispose(); will also close the connection, so can't hurt changing it to follow the dispose pattern. But there functionally equivalent so there must be a problem else where. msdn.microsoft.com/en-us/library/system.... If the SqlConnection goes out of scope, it won't be closed.

Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes, the underlying connection is returned back to the connection pool.

On the other hand, if Pooling is set to false or no, the underlying connection to the server is closed.

I think you should take it to your destructor method, and you should add a line that checks if the connection is not closed : class YourClass { ~YourClass() { if (_conn! = null && _conn. State!

= ConnectionState. Closed ) { _conn.Close(); } } } Here is a paragraph on using dispose or destructor from MSDN : If your application is using an expensive external resource, we also recommend that you provide a way to explicitly release the resource before the garbage collector frees the object. You do this by implementing a Dispose method from the IDisposable interface that performs the necessary cleanup for the object.

This can considerably improve the performance of the application. Even with this explicit control over resources, the destructor becomes a safeguard to clean up resources if the call to the Dispose method failed.

– 1800 INFORMATION Jun 19 '09 at 8:03 I updated my question about destructors and dispose. You can see MSDN uses destructor for a class finalizer. – Canavar Jun 19 '09 at 8:06 1 You will not need to implement a finalizer, since a SqlConnection already implements a finalizer trough one of its base classes (System.ComponentModel.

Component). This could actually negatively impact performance. – Dries Van Hansewijck Jun 19 '09 at 8:10 3 @Dries: Absolutely - don't create a finalizer unless you've got a direct handle reference (and then try to use SafeHandle instead anyway).

It should be very, very rare that you need to write a finalizer these days - unless it's only in debug mode, to indicate that you haven't disposed of something properly. -1 from me I'm afraid. – Jon Skeet Jun 19 '09 at 8:29.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions