In the code below, the Provider object only has one instance of a DbConnection. Each reader will reference the same connection instance. According to the Microsoft documentation, the second reader will get a second connection from the connection pool. This works correctly.
using (var reader1 as IDataReader = Provider.GetReader(sqlStatement1))
{
while(reader1.Read())
{
using (var reader2 as IDataReader = Provider.GetReader(sqlStatement2))
{
while(reader2.Read())
{
//Do stuff with both statements
}
}
}
}
I would like to the leave the connection open as long as I'm using the Provider object. However, I don't want to waste connections in the connection pool. Would calling Provider.DbConnection.Close() attempt to return both connections to the pool? If so, how could I return the second connection to the connection pool?