Re: Database connections and try catch finally?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi Gregory!

Thanks for your answer! Just what I was hoping for :-)

Regards,
Kenneth Myhra

"Cowboy (Gregory A. Beamer) - MVP" <NoSpamMgbworld@xxxxxxxxxxxxxxxxxx> wrote
in message news:7FC4DD2B-7AAB-4AAD-8DD3-8BDF4362AFF2@xxxxxxxxxxxxxxxx
> The overhead of try ... catch is very low until a problem happens, which
> is
> precisely when you need it. For this reason, I use try when I have
> database
> connections. There are a couple of patterns you can go with. The simplest
> is:
>
> try
> {
> conn.Open();
> //Do work here
> }
> finally
> {
> conn.Dispose();
> }
>
> If there is an error, clean up will still happen, but the exception is
> passed up the stack. As .NET programming is by exception (usually handled
> near the UI), this is a good basic pattern. The following accomplishes the
> same:
>
> using(SqlConnection conn)
> {
> //Create other objects
>
> conn.Open();
> //Do work
> }
>
> Using forces the IDispose.Dispose() method to be called when you exit the
> block. I tend to not use using here because of the way the code looks. As
> I
> have heard no reasoning that suggests using is superior to try ...
> finally, I
> have no reason to change. Pick your preference.
>
> IF, and only IF, you are going to handle the exception, then, do something
> like:'
>
> try
> {
> conn.Open();
> //Do work
> }
> catch (SqlException ex)
> {
> //Log the exception? or similar
> //AppSpecificException means code your own exception
> throw AppSpecificException("message", ex);
> }
> finally
> {
> conn.Dispose();
> }
>
> --
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
>
> ***************************
> Think Outside the Box!
> ***************************
>
>
> "Kenneth Myhra" wrote:
>
>> Hi guys & girls!
>>
>> We're having a discussion at work whether we should use a try catch
>> finally
>> statement when opening a database connection, so that we can close the
>> database connection in the finally statement.
>> Some of us think that the try catch finally statement uses too much
>> resources to justify using it, while other of us think that closing it in
>> the finally statement is a good think, and should always be done.
>> I also added some code in the end of this mail to describe in code what
>> our
>> disagreement is.
>>
>> So what is the expert advice?
>> Should we use try catch finally statements, or should we stop using them
>> and
>> instead rely on the .NET framework releasing our connections if an error
>> occurs before we have closed the connection?
>> Can we rely on the .NET framework to release our connections before we
>> have
>> done it explicitly ourselves?
>>
>> <code description="With try catch finally statement">
>>
>> SqlConnection conn = null;
>> try {
>> conn = new SqlConnection(...);
>> conn.Open();
>> // Additional code where an error could occur, the database connection
>> will
>> then be closed in the finally statemen...
>> }
>> catch { throw; }
>> finally {
>> if(conn != null) {
>> conn.Close()
>> conn = null;
>> }
>> // ...
>> }
>>
>> </code>
>>
>>
>> <code description="Without try catch finally statement">
>>
>> SqlConnection conn = new SqlConnection(...);
>> // Additional code where an error could occur, the database connection
>> will
>> not be closed...
>> conn.Open();
>> conn.Close()
>> conn = null;
>> // ...
>>
>> </code>
>>
>>
>> Best regards,
>> Kenneth Myhra
>> System Developer
>>
>>
>>
>>
>>


.



Relevant Pages

  • Re: Firewall and Group Policy
    ... I know that you can do this manually on servers and workstations, ... > You can configure exception that are unique per network interface. ... >> I currently have disabled MS firewall on LAN connections and enabled on ... >> when not on our LAN which has a nice hardware firewall to protect them. ...
    (microsoft.public.security)
  • Re: ExecutionEngineException in dotnet 2.0 application doing interop with VB6 legacy code
    ... all CLR exceptions use the SEH exception code e0434f4d - this is more of an historical holdover than an indication that the exception is related to COM. ... It is still a little weird that the ExecutionEngineException seems to have been the result of "just" a resource leak. ... it appears to have something to do with us not closing our database connections to sql express explicitly. ... has 800KLOC of VB6 code. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: ExecutionEngineException in dotnet 2.0 application doing interop with VB6 legacy code
    ... all CLR exceptions use the SEH exception code e0434f4d - this is more of an historical holdover than an indication that the exception is related to COM. ... It is still a little weird that the ExecutionEngineException seems to have been the result of "just" a resource leak. ... it appears to have something to do with us not closing our database connections to sql express explicitly. ... has 800KLOC of VB6 code. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Networking / exceptions.... where are they stored?
    ... You can use a Connection Manager CSP (Configuration Service Provider) to ... 03: [Connections] tab ... Could someone tell me where this exception is stored? ... exception at runtime in order to avoid manually configuring each PocketPC. ...
    (microsoft.public.pocketpc.developer.networking)
  • SV: [fw-wiz] VPN Design - is it possible
    ... PIX firewall and 2 ISP's connections as follows: ... is it possible to have 2 separate VPN connection to ... Best regards to all, ...
    (Firewall-Wizards)