Re: Database connections and try catch finally?

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



Hi Kenneth,

You should use try/finally scenario (or "using" C# keyword which does the
same).
You never know when you'll get an exception doing database IO.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Kenneth Myhra" <dontspamme@xxxxxxxxxxxxxxxx> wrote in message
news:uBGDIklzFHA.2640@xxxxxxxxxxxxxxxxxxxxxxx
> 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: Help ! -> Distributed BL and Database Access
    ... I have one server located in a remote part of the country ... method the data within the object will be comitted to the database. ... reference to the database connection and then send it to the client (by ... value - to move the work off onto the client). ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: [PHP] uh oh, I defined a resoruce
    ... I don't agree that "Global variables are evil." ... going to use only for a database connection, IMO, is perfectly fine. ... By specific globals I mean that I have a "namespaced" entry such as ...
    (php.general)
  • Re: [PHP] uh oh, I defined a resoruce
    ... I don't agree that "Global variables are evil." ... going to use only for a database connection, IMO, is perfectly fine. ... By specific globals I mean that I have a "namespaced" entry such as ...
    (php.general)
  • Why Opening a Database Connection Is Very Slow?
    ... I would like to know why this takes a long time to open a database ... connection to a SQL Server 2000 database in a server. ... I find that I may take 2 minutes or so to open a database connection. ... Dim sConnString As String ...
    (microsoft.public.pocketpc.developer)
  • Re: Transactional Adapter and Orchestration
    ... loss of the database connection during orchestration processing is my ... Since I have no way to revert the database field value ... back to the original value because I've lost the database connection, ... Microsoft Online Community Support ...
    (microsoft.public.biztalk.general)