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.