Although my assumption may seem subjective, after some research, I have found that it is not uncommon to find developers who prefer the Try/Catch dummy instead of using the Use operator to handle IDbConnection/IDbTransaction (Close / Commit / Rollback).
This is true even for some of the most experienced developers and some new ones. I intentionally do not intend to link to a single question on StackOverflow or the forum links as an example, so people are not offended. From what I found , Using the instructions is safe to use (no pun intended).
Is there something wrong with this? Consider the following code:
Public Sub Commit() Dim cn As IDbConnection = {CREATE_CONNECTION} Dim tran As IDbTransaction = Nothing cn.Open() Try tran = cn.BeginTransaction 'run some queries here tran.Commit() Catch ex As Exception If Not tran Is Nothing Then tran.Rollback() Throw Finally cn.Close() End Try End Function
Assume that {CREATE_CONNECTION} is the owner of the Sub space, which creates a connection, depending on the database provider, written in accordance with all possible best practices and not requiring much improvement.
Is there a reason why the above code cannot be rewritten as such:
Using cn As IDbConnection = {CREATE_CONNECTION} cn.Open() Using tran As IDbTransaction = cn.BeginTransaction 'run some queries here tran.Commit() End Using End Using
Obviously, version number 2 is more intuitive for what it does. But maybe I am missing something important here? Things like vendor-specific data access library implementations that don't call Transaction.Commit and / or Connection.Close on Dispose internally? Is this approach decommissioned in the near future or is it not considered clear enough in the current programming scheme / best practice? Mono / mobile dev applications do not have debugging support for the Using keyword
I am looking for any answer to support or deny this. Preferably, one who has quotes for the original documentation, something like Do not use Using with IDbTransaction when ... Links to blogs or personal experiences are fine too.
Neolisk
source share