Question

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?

Was it helpful?

Solution

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();
}

OTHER TIPS

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.

Yeah, the Dispose will be called. the using statement only works with disposable objects. Like this:

using (DbProviderConnection cnctn = _planDb.CreateOpenConnection())
{
    using (cnctn.BeginTransaction())
    {
        // ...
        cnctn.Transaction.Commit();
    } // Here BeginTransaction.Dispose() is called.
} // Here DbProviderConnection.Dispose() is called.

The object that BeginTransaction returns, is what will be disposed.

BeginTransaction returns a DbTransaction so that is what will be disposed

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top