Simple question: Best practice to open and use database

I'm using VB.NET 2005. I would like to know what's the best practice to open and use database

First, is there any different in calling the dispose method explicitly and the "using" statement Is the "using" statement better (Cleaner code )

Second, will the GC or .NET runtime know how to clear up the connection, command or datareader objects(all I should say unmanaged object) if I haven't called the dispose method or use the "using" statement Will those objects call the dispose method implicitly when they're out of scope (I don't think so)

Third, how about if I open an unmanaged object but haven't called the close method What's the default behaviour

Forth, how can I detect these kind of resource leaks efficiently

Alex




Answer this question

Simple question: Best practice to open and use database

  • dihoch

    Yes, that is correct. If the connection is still open when it is disposed, it will be closed.

  • Ravs Kaur _ MS

    Dispose and Close are equivalent with connection classes.

    Using statement is good idea, because objects that go out of scope are not cleaned up right away.

    The simplest way to tell if you have a connection leak is use the SQL Server performance counters and look at User Connections. If user connections go up and up then you are typically leaking connections. Also look at logins/sec, if pooling is working properly and you are closing connections properly logins/sec should drop to zero under load. These perfmon counters are under SQLServer:General Statistics I believe.

    With ADO.NET there is a limit called Max Connections as well the default is 100 under load you will hit this very quickly if you are not properly closing connections.



  • atmaj.desai

    I've gone through some materials on the web but still get a bit confuse. For question 1, I just like to know whether it's recommended to use the "using" statement.

    MSDN: "The Using block in Visual Basic or C# automatically disposes of the connection when the code exits the block, even in the case of an unhandled exception. You can also use the Close or Dispose methods of the connection object for the provider that you are using."

    I'm confuse about dispose and close. Does it mean dispose will also close the connection If I'm using the "using" statement, then I don't need both close and dispose statement. Is that correct



  • Uwe Lesta

    Simple question, huh

    There are many, MANY articles on this topic on the internet. But when in doubt, go straight to the source (MSDN, that is):

    1. http://msdn2.microsoft.com/en-US/library/yh598w02.aspx
    2. http://msdn2.microsoft.com/en-US/library/ms254507(VS.80).aspx
    3. Not sure what you mean by this...
    4. What kind of resource leaks, specifically You mean leaked connections I aim not aware of any simple method which can detect a connection leak, primarily because its difficult for any external diagnostic program to know whether a given connection is still being used or not.



  • Simple question: Best practice to open and use database