I have just completed reading a chapter in my book on performance. The books says to speed up performance on your web site you should use the using statement when opening connections. My question is how do you catch errors if you are using this as apposed to a try catch block.
thanks
Bryan
![]() |
-2 |
![]() |
Why would you use it as an alternative to a try/catch statement. Couldn't you just do something like
public void UsingSomeMethod() {
try {
SomeMethod();
}
catch(SomeException e) {
/// do something useful
}
}
public void SomeMethod() {
using (Something) {
/// do something
}
}
![]() |
2 |
![]() |
public void SomeMethod()
{
using (Something){
try {}
catch(SomeException e) {}
}
}
![]() |
2 |
![]() |
Hi,
So what is the actual benefit of using the using statement if you still have to put it in a try catch to catch errors, I thought that was the point of using using... to get away from the try catch which is apparently more of a memory chomper. Will it really speed up my apps then? The only benefit i can see it that you dont have to explicitly close your connection which would free up some memory there.
Bryan
![]() |
0 |
![]() |
First of all there are two things, using Statement and using directive, I think you are referring to the 'using' statement.
The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources from http://msdn2.microsoft.com/en-us/library/yh598w02(VS.80).aspx
Now let me give a more precise example to explain this.. Imagine you opened an sql connection, and you forgot to call a dispose() on it i.e. to destroy the connection object, a lack of calling Dispose() on SqlConnection and SqlCommand objects and further imagine that the database code was not placed in try / finally blocks.
SqlConnection cn = new SqlConnection(connectionString);
SqlCommand cm = new SqlCommand(commandString, cn);
cn.Open();
cm.ExecuteNonQuery();
cn.Close();
It sure is easy to follow :)The problem is that SqlConnection and SqlCommand implement IDisposable, which means they could have unmanaged resources to cleanup and it is our job, the developers, to make sure Dispose() gets called on these classes after we are finished with them. And, because an exception could be raised if the database is unavailable, we need to make sure Dispose() gets called even in the case of an exception.
Internally, this construct generates a try / finally around the object being allocated and calls Dispose() for you. It saves you the hassle of manually creating the try / finally block and calling Dispose().
The new code would looking something like this:
using (SqlConnection cn = new SqlConnection(connectionString))
{
using (SqlCommand cm = new SqlCommand(commandString, cn))
{
cn.Open();
cm.ExecuteNonQuery();
}
}This is essentially equivalent to the following:
SqlConnection cn = null;
SqlCommand cm = null;try
{
cn = new SqlConnection(connectionString);
cm = new SqlCommand(commandString, cn);
cn.Open();
cm.ExecuteNonQuery();
}
finally
{
if (null != cm);
cm.Dispose();
if (null != cn)
cn.Dispose();
}You may notice the lack of calling Close() on the SqlConnection class, cn. Internally, Dispose() checks the status of the connection and closes it for you. Therefore, technically you don't need to call Close() on the connection (cn) as Dispose() will do it for you.
In addition, Dispose() destroys the connection string of the SqlConnection class. Therefore, if you want to re-open the connection after calling Dispose() on cn, you will have to re-establish the connection string. Not doing so will throw an exception by http://davidhayden.com/blog/dave/archive/2005/01/13/773.aspx
Please Mark Post that helped you as answer, also include a summary of what solved the problem as it helps others in similar situations
![]() |
0 |
![]() |
subhendude:
public void SomeMethod()
{
using (Something){
try {}
catch(SomeException e) {}
}
}This code makes no sense!
"using" keyword defines a scope, outside of which the objects will be disposed. (The objects provided to the using statement must implement the IDisposable interface to call the Dispose method). Internally a try/finally block will be generated automatically around the object when you use it with the "using" statement and the Dispose method is called inside the finally block.
Thanks
More: http://msdn2.microsoft.com/en-us/library/3bwa4xa9.aspx
-Mark post(s) as "Answer" that helped you
Mark post(s) as "Answer" that helped you
Electronic Screw
Website||Blog||Dub@i.net
![]() |
0 |
![]() |
naturehermit:
Now let me give a more precise example to explain this.. Imagine you opened an sql connection, and you forgot to call a dispose() on it i.e. to destroy the connection object, a lack of calling Dispose() on SqlConnection and SqlCommand objects and further imagine that the database code was not placed in try / finally blocks........Code taken from David's blog. You should have attributed with the original entry http://davidhayden.com/blog/dave/archive/2005/01/13/773.aspx
Thanks
Mark post(s) as "Answer" that helped you
Electronic Screw
Website||Blog||Dub@i.net
![]() |
0 |
![]() |
Hi,
I get all of this but i thought that there would be another construct to allow you to catch your error when using athe using statement. From your above example what i can see is that you still need to encapsulate your using code in a try catch to catch any potential error that may crop up.
I thought that the using statement was a substitute for a try catch block with its own error handling, but i was wrong. Thanks for the awesome explination. I will be adding this to my new projects.
Thanks A mill!
Bryan
![]() |
0 |
![]() |