KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
While my assumption may seem to sound subjective, after some research, I found that it's not uncommon to find developers who favour a dummy Try/Catch instead of using the Using statement for IDbConnection/IDbTransaction processing (Close/Commit/Rollback). This holds true for even some of the most seasoned developers and some new ones. I am intentionally not going to reference any of the question on StackOverflow or forum links as an example, so people don't get offended. From what I found , Using statement is safe to use (no pun intended). Is there anything wrong with it? 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 {CREATE_CONNECTION} is place holder for a Sub that creates a connection, depending on the database vendor, written according to all possible best practices and does not need more 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() En
Tags (comma-separated)
Save Edits
Cancel