Skip to content

Commit

Permalink
Add: feature 106; dapper takes control of the connection if needeed
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Oct 9, 2012
1 parent 293a8ed commit 6ae42c2
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 26 deletions.
82 changes: 56 additions & 26 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,16 +786,28 @@ public static GridReader QueryMultiple(

IDbCommand cmd = null;
IDataReader reader = null;
bool wasClosed = cnn.State == ConnectionState.Closed;
try
{
if (wasClosed) cnn.Open();
cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, (object)param, commandTimeout, commandType);
reader = cmd.ExecuteReader();
reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection : CommandBehavior.Default);
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
// still need something in the "finally" to ensure that broken SQL still results
// in the connection closing itself
return new GridReader(cmd, reader, identity);
}
catch
{
if (reader != null) reader.Dispose();
if (reader != null)
{
if (!reader.IsClosed) try { cmd.Cancel(); }
catch { /* don't spol the existing exception */ }
reader.Dispose();
}
if (cmd != null) cmd.Dispose();
if (wasClosed) cnn.Close();
throw;
}
}
Expand All @@ -808,36 +820,45 @@ private static IEnumerable<T> QueryInternal<T>(this IDbConnection cnn, string sq
var identity = new Identity(sql, commandType, cnn, typeof(T), param == null ? null : param.GetType(), null);
var info = GetCacheInfo(identity);

using (var cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param, commandTimeout, commandType))
IDbCommand cmd = null;
IDataReader reader = null;

bool wasClosed = cnn.State == ConnectionState.Closed;
try
{
IDataReader reader = null;
try
cmd = SetupCommand(cnn, transaction, sql, info.ParamReader, param, commandTimeout, commandType);

if (wasClosed) cnn.Open();
reader = cmd.ExecuteReader(wasClosed ? CommandBehavior.CloseConnection : CommandBehavior.Default);
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
// still need something in the "finally" to ensure that broken SQL still results
// in the connection closing itself
var tuple = info.Deserializer;
int hash = GetColumnHash(reader);
if (tuple.Func == null || tuple.Hash != hash)
{
reader = cmd.ExecuteReader();
var tuple = info.Deserializer;
int hash = GetColumnHash(reader);
if (tuple.Func == null || tuple.Hash != hash)
{
tuple = info.Deserializer = new DeserializerState(hash, GetDeserializer(typeof(T), reader, 0, -1, false));
SetQueryCache(identity, info);
}
tuple = info.Deserializer = new DeserializerState(hash, GetDeserializer(typeof(T), reader, 0, -1, false));
SetQueryCache(identity, info);
}

var func = tuple.Func;
var func = tuple.Func;

while (reader.Read())
{
yield return (T)func(reader);
}
while (reader.Read())
{
yield return (T)func(reader);
}
finally
}
finally
{
if (reader != null)
{
if (reader != null)
{
if (!reader.IsClosed) try { cmd.Cancel(); }
catch { /* don't spol the existing exception */ }
reader.Dispose();
}
if (!reader.IsClosed) try { cmd.Cancel(); }
catch { /* don't spol the existing exception */ }
reader.Dispose();
}
if (wasClosed) cnn.Close();
if (cmd != null) cmd.Dispose();
}
}

Expand Down Expand Up @@ -1671,10 +1692,19 @@ private static IDbCommand SetupCommand(IDbConnection cnn, IDbTransaction transac

private static int ExecuteCommand(IDbConnection cnn, IDbTransaction transaction, string sql, Action<IDbCommand, object> paramReader, object obj, int? commandTimeout, CommandType? commandType)
{
using (var cmd = SetupCommand(cnn, transaction, sql, paramReader, obj, commandTimeout, commandType))
IDbCommand cmd = null;
bool wasClosed = cnn.State == ConnectionState.Closed;
try
{
cmd = SetupCommand(cnn, transaction, sql, paramReader, obj, commandTimeout, commandType);
if (wasClosed) cnn.Open();
return cmd.ExecuteNonQuery();
}
finally
{
if (wasClosed) cnn.Close();
if (cmd != null) cmd.Dispose();
}
}

private static Func<IDataReader, object> GetStructDeserializer(Type type, Type effectiveType, int index)
Expand Down
81 changes: 81 additions & 0 deletions Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2015,6 +2015,87 @@ public Issue40_User()
public bool Active { get; set; }
}

SqlConnection GetClosedConnection()
{
var conn = new SqlConnection(connection.ConnectionString);
if (conn.State != ConnectionState.Closed) throw new InvalidOperationException("should be closed!");
return conn;
}
public void ExecuteFromClosed()
{
using (var conn = GetClosedConnection())
{
conn.Execute("-- nop");
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
public void ExecuteInvalidFromClosed()
{
using (var conn = GetClosedConnection())
{
try
{
conn.Execute("nop");
false.IsEqualTo(true); // shouldn't have got here
}
catch
{
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
}
public void QueryFromClosed()
{
using (var conn = GetClosedConnection())
{
var i = conn.Query<int>("select 1").Single();
conn.State.IsEqualTo(ConnectionState.Closed);
i.IsEqualTo(1);
}
}
public void QueryInvalidFromClosed()
{
using (var conn = GetClosedConnection())
{
try
{
conn.Query<int>("select gibberish").Single();
false.IsEqualTo(true); // shouldn't have got here
}
catch
{
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
}
public void QueryMultipleFromClosed()
{
using (var conn = GetClosedConnection())
{
using (var multi = conn.QueryMultiple("select 1; select 'abc';"))
{
multi.Read<int>().Single().IsEqualTo(1);
multi.Read<string>().Single().IsEqualTo("abc");
}
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
public void QueryMultipleInvalidFromClosed()
{
using (var conn = GetClosedConnection())
{
try
{
conn.QueryMultiple("select gibberish");
false.IsEqualTo(true); // shouldn't have got here
}
catch
{
conn.State.IsEqualTo(ConnectionState.Closed);
}
}
}

class TransactedConnection : IDbConnection
{
IDbConnection _conn;
Expand Down

0 comments on commit 6ae42c2

Please sign in to comment.