Commit 24225931 authored by Marc Gravell's avatar Marc Gravell

Pull request 55 (thanks: haeal), with some tweaks; better handling of shutting...

Pull request 55 (thanks: haeal), with some tweaks; better handling of shutting down the data-reader on the happy path
parent 6ae42c29
...@@ -792,11 +792,13 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn ...@@ -792,11 +792,13 @@ public static IEnumerable<dynamic> Query(this IDbConnection cnn, string sql, dyn
if (wasClosed) cnn.Open(); if (wasClosed) cnn.Open();
cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, (object)param, commandTimeout, commandType); cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, (object)param, commandTimeout, commandType);
reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection : CommandBehavior.Default); reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection : CommandBehavior.Default);
var result = new GridReader(cmd, reader, identity);
wasClosed = false; // *if* the connection was closed and we got this far, then we now have a reader wasClosed = false; // *if* the connection was closed and we got this far, then we now have a reader
// with the CloseConnection flag, so the reader will deal with the connection; we // with the CloseConnection flag, so the reader will deal with the connection; we
// still need something in the "finally" to ensure that broken SQL still results // still need something in the "finally" to ensure that broken SQL still results
// in the connection closing itself // in the connection closing itself
return new GridReader(cmd, reader, identity); return result;
} }
catch catch
{ {
...@@ -848,6 +850,10 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq ...@@ -848,6 +850,10 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq
{ {
yield return (T)func(reader); yield return (T)func(reader);
} }
// happy path; close the reader cleanly - no
// need for "Cancel" etc
reader.Dispose();
reader = null;
} }
finally finally
{ {
...@@ -2418,6 +2424,11 @@ private void NextResult() ...@@ -2418,6 +2424,11 @@ private void NextResult()
} }
else else
{ {
// happy path; close the reader cleanly - no
// need for "Cancel" etc
reader.Dispose();
reader = null;
Dispose(); Dispose();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment