tags:

views:

968

answers:

4

Here is a sample code to retrieve data from a database using the yield keyword that I found in a few place while googling around :

public IEnumerable<object> ExecuteSelect(string commandText)
{
    using (IDbConnection connection = CreateConnection())
    {
        using (IDbCommand cmd = CreateCommand(commandText, connection))
        {
             connection.Open();
             using (IDbDataReader reader = cmd.ExecuteReader())
             {
                while(reader.Read())
                {
                    yield return reader["SomeField"];
                }
             }
             connection.Close();
        }
    }
}

Am I correct in thinking that in this sample code, the connection would not be closed if we do not iterate over the whole datareader ?

Here is an example that would not close the connection, if I understand yield correctly..

foreach(object obj in ExecuteSelect(commandText))
{
  break;
}

For a db connection that might not be catastrophic, I suppose the GC would clean it up eventually, but what if instead of a connection it was a more critical resource?

+2  A: 

Connection will be closed automatically since you're using it inside "using" block.

aku
A: 

Judging from this technical explanation, your code will not work as expected, but abort on the second item, because the connection was already closed when returning the first item.

@Joel Gauvreau : Yes, I should have read on. Part 3 of this series explains that the compiler adds special handling for finally blocks to trigger only at the real end.

David Schmitt
+2  A: 

From the simple test I have tried, aku is right, dispose is called as soon as the foreach block exit.

@David : However call stack is kept between call, so the connection would not be closed because on the next call we would return to the next instruction after the yield, which is the while block.

My understanding is that when the iterator is disposed, the connection would also be disposed with it. I also think that the Connection.Close would not be needed because it would be taken care of when the object is disposed because of the using clause.

Here is a simple program I tried to test the behavior...

class Program
{
    static void Main(string[] args)
    {
        foreach (int v in getValues())
        {
            Console.WriteLine(v);
        }
        Console.ReadKey();

        foreach (int v in getValues())
        {
            Console.WriteLine(v);
            break;
        }
        Console.ReadKey();
    }

    public static IEnumerable<int> getValues()
    {
        using (TestDisposable t = new TestDisposable())
        {
            for(int i = 0; i<10; i++)
                yield return t.GetValue();
        }
    }
}

public class TestDisposable : IDisposable
{
    private int value;

    public void Dispose()
    {
        Console.WriteLine("Disposed");
    }

    public int GetValue()
    {
        value += 1;
        return value;
    }
}
Joel Gauvreau
+4  A: 

The Iterator that the compiler synthesises implements IDisposable, which foreach calls when the foreach loop is exited.

The Iterator's Dispose() method will clean up the using statements on early exit.

As long as you use the iterator in a foreach loop, using() block, or call the Dispose() method in some other way, the cleanup of the Iterator will happen.

Douglas Leeder