I've always used using with variable and assignment. Now i have like this a class DbProviderConnection:
public class DbProviderConnection : IDisposable { public DbConnection Connection { get; set; } public DbTransaction Transaction { get; set; } public DbTransaction BeginTransaction() { Transaction = Connection.BeginTransaction(); return Transaction; } //... and so on }
Now i was thinkin to use it like this:
using (DbProviderConnection cnctn = _planDb.CreateOpenConnection()) { using (cnctn.BeginTransaction()) { //... cnctn.Transaction.Commit(); } }
My question is: Is the DbProviderConnection.Transaction.Dispose
called?
The using statement guarantees that the object is disposed in the event an exception is thrown. It's the equivalent of calling dispose in a finally block.
Answers. We don't dispose the variables. In case if there are any unmanaged resources (or the ones which are inherited from IDisposable) can have Dispose() calls. In your case, you may have simple string or other primitive datatypes - these are not the ones which implement IDisposable interface.
C# provides a special "using" statement to call Dispose method explicitly. using statement gives you a proper way to call the Dispose method on the object. In using statement, we instantiate an object in the statement. At the end of using statement block, it automatically calls the Dispose method.
The using statement is used to set one or more than one resource. These resources are executed and the resource is released. The statement is also used with database operations. The main goal is to manage resources and release all the resources automatically.
From C# Specification 8.13 using statement defined as
using-statement: using (resource-acquisition) embedded-statement
Where resource-acquisition is
resource-acquisition: local-variable-declaration expression
In first case you have using which acquires resource via local variable declaration. In second case resource is acquired via expression. So, in second case resouce will be result of cnctn.BeginTransaction()
call, which is DbTransaction
from your DbProviderConnection
class. Using statement disposes its resource after usage. So, yes, DbProviderConnection.Transaction.Dispose()
will be called.
UPDATE: According to same article, your second using block will be translated to
DbTransaction resource = cnctn.BeginTransaction(); try { //... cnctn.Transaction.Commit(); } finally { if (resource != null) ((IDisposable)resource).Dispose(); }
From the specification:
8.13 The using statement
A using statement of the form
using (ResourceType resource = expression) statement
when ResourceType is a nullable value type or a reference type other than dynamic, the expansion is
{ ResourceType resource = expression; try { statement; } finally { if (resource != null) ((IDisposable)resource).Dispose(); } }
A using statement of the form
using (expression) statement
has the same three possible expansions...The resource variable is inaccessible in, and invisible to, the embedded statement.
Therefore the object returned from cnctn.BeginTransaction()
will be disposed when the block exits, but is not accessible inside the associated block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With