Reputation: 53
I am a beginner for .NET. .NET says memory deallocation is automatically handled, but i have read in many blogs that finally block is to be used for dbconnections to dispose.If it is automatically handled,why there is a need for this. Or,.NET automatically handling is only for some resources which excludes dbconnections?. Please clarify..
Upvotes: 1
Views: 101
Reputation: 93434
As a beginner, you really don't need to concern yourself too much with this. You will eventually have to learn more about it, but at this stage you need to understand that managed memory is handled by the framework. You need to keep in mind that when you reference memory, it will continue to be kept around until all references have been released, then when the garbage collector runs that memory will get cleaned up automatically.
There are some objects which require outside resources, such as files, database connections, network connections (including web requests), bitmaps and fonts, etc.. things which the Operating system itself tracks. These objects typically implement an interface called IDisposable, which is used to dispose of any unmanaged resources.
Basically, you look at the documentation of an object, and if it implements IDisposable, then you want to dispose of it as soon as it is feasible for you. This is done by calling Dispose() on it, or implicitly with the using statement.
If you wrap your db connection in a using statement, then no finally is necessary, as using interally is implemented with a finally. If you don't use a using statement, you may need to use a finally to call dispose. It's largely up to you.
Examples:
using (var dbconn = new SqlConnection()) {
// do you data access - no finally required
}
or
SqlConnection dbconn;
try {
dbconn = new SqlConnection();
// do your data access
}
catch (... various exceptions) {}
finally
{
if (dbconn != null) {
dbconn.Close();
dbconn.Dispose();
}
}
Upvotes: 2
Reputation: 172438
finally block is executed for cleaning up of resources. Control is passed to the finally block to get rid of the resources. The Garbage Collector will do this for you. But there are many times when you explicitly want a piece of code to be executed before the GC comes into picture.
Sometimes there are scenarios when you want certain piece of your code to be executed even when there is an exception then also finally comes into picture.
Upvotes: -1
Reputation: 11104
You should read first Managed code and Unmanaged code..
(dbconnections)System.Data.SqlClient itself is managed code. However, it is built on top of several other libraries. Some of them are managed code and some are not.
The query itself is executed by a database. So, the text of the query (the part that is hard-coded in your program) would be considered managed code, but once the command is sent to a database, it is no-longer managed.
Once the .close command is executed, the connection itself is released from your .net app and returned to the connection pool (part of OLEDB, ODBC and/or other sub-systems). At that point, the connection is freed and the unmanaged part is released. Dispose will do everything close does and release other managed parts (connection string, etc).
It is always a good idea to call dispose on any object that implements it (or wrap it in a using block, like @IRSOG showed). In this case, it isn't particularly dangerous to just call close(). I wouldn't make a habit of it.
or use (using )
block here is more info http://www.codeproject.com/Articles/6564/Understanding-the-using-statement-in-C
using (MyResource myRes = new MyResource())
{
myRes.DoSomething();
}
Upvotes: 0