Should i dispose sqlconnection




















If you don't call Dispose on your objects any unmanaged resources that they used won't be disposed until the garbage collector comes around and calls finalize on them and who knows when that will happen.

This scenario is called Non Deterministic Finalization and is a common trap for. If you're working with objects that implement IDisposable then call Dispose on them! While there may be many instances like on SqlConnection where you call Disponse on some object and it simply calls Close on it's connection or closes a file handle, it's almost always your best bet to call Dispose!

For SqlConnection , from the perspective of the connection itself, they are equivalent. According to Reflector, Dispose calls Close as well as doing a few additional memory-freeing operations -- mostly by setting members equal to null. As tyler pointed out in his nice answer, calling Dispose is a great programming practice. This is because this method is supposed to "rally together" all the resource-freeing needed so there are no unneeded open resources.

If you wrote some text to a file, for example, and failed to close the file free the resource , it will remain open and no one else will be able to write to it until the GC comes around and does what you should have done. Now, in some cases there will be "finalizing" methods more specific to the class you're dealing with, like StreamWriter. Close , which overrides TextWriter.

Indeed they are usually more suited to the situation: a StreamWriter's Close , for example, flushes the stream and the underlying encoder before Dispose ing of the object! However, browsing MSDN you'll find that even Microsoft is sometimes confused by the multitude of closers and disposers. In this webpage , for instance, in some examples Close is called before the implicit Dispose see using statement if you don't understand why it's implicit , and in one in particular they don't bother to.

Why would that be? I too was perplexed. The reason I figured and, I stress, this is original research and I surely might lose reputation if I'm wrong is that Close might fail, yielding an exception whilst leaving resources open, while Dispose would surely free them. Which is why a Dispose should always safeguard a Close call sorry for the pun.

And yes, I guess Microsoft slipped on that one example. Perhaps that timestamp would never get flushed to the file. Edit: sorry Brannon, I can't comment on your answer, but are you sure it's a good idea to call Close on a finally block? I guess an exception from that might ruin the rest of the block, which likely would contain important cleanup code. Reply to Brannon's: great, just don't forget to call Close when it is really needed e. Typecast to iDisposable, and call dispose on that.

That will invoke whatever method is configured as implementing "iDisposable. Dispose", regardless of what the function is named. Generally we are facing issue in Close , Abort and Dispose but let me tell you difference among them. If you having bulk request then you can't use abort because it may caused time out for your limited connection pool. Here, one more thing to look. In some cases, if error generates then it is not a good way to write code in finally that connection.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Learn more. Close and Dispose - which to call? Ask Question. Asked 13 years, 2 months ago. Active 3 years, 9 months ago. Viewed k times. Improve this question. Community Bot 1 1 1 silver badge. Add a comment. Active Oldest Votes. I want to clarify this situation.

Brad Abrams discussed this on his blog. The design guidelines book recommends making Close and Dispose aliases for each other, so either can be used interchangably.

I don't know your source Connections are released back into the pool when you call Close or Dispose on the Connection Learn More. Ask a question. Quick access. Search related threads. Remove From My Forums. Answered by:.

Archived Forums. Also discuss all the other Microsoft libraries that are built on or extend the. Also and this is really important , please, use the "using" keyword. That way, you will not have to deal with closing the connection and cleanup, because your code as it is written now doesn't deal with closing the connections, so in a case of some exception you might end up with hitting connection limit on your server.

Go with something like this:. As you can see, there is no need to call connection. Close or deal with exceptions and closing the connection in your finally block, because that is a "job" for the "using" block. Also, one important note It seems that people are confused as to whether this is seconds or milliseconds. The documentation states that the timeout is in seconds. A 1-minute timeout seems reasonable for most queries. Do you have queries you're expecting to take longer than a minute?

If so, raise it to a value you expect to be higher than anything you'd see if everything is working properly, but not so high as to take forever to alert you to a problem. For example, you might go from 1 minute to 5 minutes. Is it necessary to dispose SqlConnection as well as SqlCommand?

Asked 3 Months ago Answers: 5 Viewed 21 times. Is it necessary to separately dispose of a SqlConnection as well as a SqlCommand? StoredProcedure; command. I'm not sure about the interaction of commands on these two connections, since they're running against the same database, but I think errors can occur if commands are issued on both at the same time: errors like "Transaction context in use by another session" Q3.

More details here Also and this is really important , please, use the "using" keyword. Only authorized users can answer the question. Please sign in first, or register a free account.



0コメント

  • 1000 / 1000