I'm looking at modifying an old school WebMethod in ASMX and for the Oracle connections, I am seeing these finally blocks repeated a thousand times. I eventually want to c开发者_JS百科onvert this over to WCF but for now what is wrong with this ?
finally
{
if (command != null)
{
command.Dispose();
command = null;
}
if (connection != null)
{
connection.Close();
connection.Dispose();
connection = null;
}
if (adapter != null)
{
adapter.Dispose();
adapter = null;
}
}
The idea is to clean up everything related to the connection to the Oracle database and any commands that were executed against it as well as any data adapters that were used.
Having this code in the finally
block ensures that the connections are closed and the resources used by their invocation are disposed of and garbage collected.
There's nothing wrong with it, but this logic could be abstracted away so it doesn't have to be repeated over and over again.
精彩评论