开发者

Why is the finally block in ASMX WebMethod written with Close and Dispose

开发者 https://www.devze.com 2023-03-11 23:53 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号