0

its been my practice to have the try-catch block inside of a using block rather than its vice versa. However, recently, i have been told that it wasn't the right way to go. As they've said the Database Connection does not close properly at this approach.

Below is an example block of how i implement it so:

using(var dbConn = new MySqlConnection(dbConnStr)) {
    using(var dbTrans = dbConn.BeginTransaction()) {
       try {
           ...
           ...
           ...

           return some_results_here;
        }
        catch(Exception Ex) {
            throw;
        }
    }
}

Accordingly, i was told that neither the dbConn nor the dbTrans automatically disposes (contrary to my belief as they are both disposable and in a using statement) and the connection is left open. Now i am confused and frustrated if it really is the case so. As this has been my practice on almost all the projects i have been working with and i have not encountered such issues with.

Please clear me up with this myth as so. Thank you all.

Nii
  • 450
  • 6
  • 25
  • 2
    I can't see why they wouldn't be properly closed here. Can whoever is telling you this elaborate on why they think this? A `using` block basically compiles down to a `try/finally` block, and `finally` always executes when leaving that block. (With the exception of extreme examples, such as critical system failures or power loss.) Perhaps whoever is making this claim can demonstrate with an actual example why it's bad? – David Aug 03 '17 at 12:32
  • 1
    Whoever told you that is wrong. A `using` statement implements `IDisposable` and thus will dispose of it properly. – Adam Aug 03 '17 at 12:33
  • Either your collegues don´t know that enclosing an `IDisposable` into a `using`-block *automatically* calls `Dispose` and thus `Close` (if existing) , or they just didn´t read the code carefully. Either way your connection *will* get closed. – MakePeaceGreatAgain Aug 03 '17 at 12:33
  • 1
    Also you should remove your try catch. Your try catch, as shown in your code, does absolutely nothing. – mjwills Aug 03 '17 at 12:34
  • Possible duplicate of [Uses of "using" in C#](https://stackoverflow.com/questions/75401/uses-of-using-in-c-sharp) – mjwills Aug 03 '17 at 12:40
  • Have you tested if the connection remains active after the exception is thrown? As the assertion here is that the throw stops further progression, therefore the using blocks never get disposed, correct? According to the MSDN documents, the using statement will still process the finally block even if an exception is thrown inside a using block http://msdn.microsoft.com/en-us/library/yh598w02.aspx -- I can't for certain tell you which frameworks this does not apply to though. So, I don't want to say this is 100% going to be the case... – Svek Aug 03 '17 at 12:47

3 Answers3

3

It doesn't matter if you put a try...catch before or after the using, since using is just a shorthand for a try...finally with a call to dispose.

Using is similar to this:

IDisposable d = (the assignment);

try
{
}
finally
{
    if (d != null)
        d.Dispose();
}

Besides some edge cases, in both cases your using instances will get disposed.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

Patrick's answer perfectly clears up the question, however as additional support, directly from MSDN...

As a rule, when you use an IDisposable object, you should declare and instantiate it in a using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):

You can also easily find a discussion on this: Here. In summary, it will dispose.

Adam
  • 2,422
  • 18
  • 29
0

To answer your question specifically on the assertion that the throw inside the using block stops further progression of code getting run, (therefore the resources in your using blocks never get disposed)...

According to the Microsoft Docs

"The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. "

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement

Note: I cannot be 100% certain this applies to every version of the .NET framework. If in doubt, I would suggest testing.

Svek
  • 12,350
  • 6
  • 38
  • 69