Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions Dapper/SqlMapper.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -934,17 +934,26 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TFirst, TSecond, T
var identity = new Identity<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh>(command.CommandText, command.CommandTypeDirect, cnn, typeof(TFirst), param?.GetType());
var info = GetCacheInfo(identity, param, command.AddToCache);
bool wasClosed = cnn.State == ConnectionState.Closed;
DbCommand? cmd = null;
DbDataReader? reader = null;
try
{
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
using var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
using var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, command.CancellationToken).ConfigureAwait(false);
if (!command.Buffered) wasClosed = false; // handing back open reader; rely on command-behavior
cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, command.CancellationToken).ConfigureAwait(false);
var results = MultiMapImpl<TFirst, TSecond, TThird, TFourth, TFifth, TSixth, TSeventh, TReturn>(null, CommandDefinition.ForCallback(command.Parameters, command.Flags), map, splitOn, reader, identity, true);
return command.Buffered ? results.ToList() : results;
if (command.Buffered) return results.ToList();

var deferred = ExecuteReaderSync(results, reader, cmd, cnn, wasClosed);
reader = null;
cmd = null;
wasClosed = false;
return deferred;
}
finally
{
reader?.Dispose();
cmd?.Dispose();
if (wasClosed) cnn.Close();
}
}
Expand Down Expand Up @@ -983,20 +992,44 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TReturn>(this IDbC
var identity = new IdentityWithTypes(command.CommandText, command.CommandTypeDirect, cnn, types[0], param?.GetType(), types);
var info = GetCacheInfo(identity, param, command.AddToCache);
bool wasClosed = cnn.State == ConnectionState.Closed;
DbCommand? cmd = null;
DbDataReader? reader = null;
try
{
if (wasClosed) await cnn.TryOpenAsync(command.CancellationToken).ConfigureAwait(false);
using var cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
using var reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, command.CancellationToken).ConfigureAwait(false);
cmd = command.TrySetupAsyncCommand(cnn, info.ParamReader);
reader = await ExecuteReaderWithFlagsFallbackAsync(cmd, wasClosed, CommandBehavior.SequentialAccess | CommandBehavior.SingleResult, command.CancellationToken).ConfigureAwait(false);
var results = MultiMapImpl(null, default, types, map, splitOn, reader, identity, true);
return command.Buffered ? results.ToList() : results;
}
finally
{
if (wasClosed) cnn.Close();
if (command.Buffered) return results.ToList();

var deferred = ExecuteReaderSync(results, reader, cmd, cnn, wasClosed);
reader = null;
cmd = null;
wasClosed = false;
return deferred;
}
finally
{
reader?.Dispose();
cmd?.Dispose();
if (wasClosed) cnn.Close();
}
}

private static IEnumerable<T> ExecuteReaderSync<T>(IEnumerable<T> results, DbDataReader reader, DbCommand command, IDbConnection cnn, bool wasClosed)
{
try
{
foreach (var result in results) yield return result;
}
finally
{
reader.Dispose();
command.Dispose();
if (wasClosed) cnn.Close();
}
}

private static IEnumerable<T> ExecuteReaderSync<T>(DbDataReader reader, Func<DbDataReader, object> func, object? parameters)
{
using (reader)
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<PackageVersion Include="RepoDb.SqlServer" Version="1.13.1" />
<PackageVersion Include="ServiceStack.OrmLite.SqlServer" Version="10.0.6" />
<PackageVersion Include="Snowflake.Data" Version="5.5.0" />
<PackageVersion Include="pengdows.crud.fakeDb" Version="2.0.5" />
<PackageVersion Include="SqlMarshal" Version="0.5.0" />
<PackageVersion Include="SubSonic" Version="3.0.0.4" />
<PackageVersion Include="Susanoo.SqlServer" Version="1.2.4.2" />
Expand Down
1 change: 1 addition & 0 deletions tests/Dapper.Tests/Dapper.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net481'">
<PackageReference Include="pengdows.crud.fakeDb" />
<PackageReference Include="Npgsql" />
<PackageReference Include="Snowflake.Data" />
</ItemGroup>
Expand Down
32 changes: 32 additions & 0 deletions tests/Dapper.Tests/FakeDbTests.Issue2099.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#if !NET481
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using pengdows.crud.fakeDb;
using Xunit;

namespace Dapper.Tests;

public sealed class FakeDbIssue2099Tests
{
[Fact]
public async Task UnbufferedAsyncMultiMapKeepsReaderAliveUntilEnumeration()
{
using var connection = new fakeDbConnection(new FakeDataStore());
connection.EnqueueReaderResult(new[]
{
new Dictionary<string, object?> { ["A"] = 1, ["B"] = 2 },
});
connection.Open();

var command = new CommandDefinition("select 1 as A, 2 as B", flags: CommandFlags.None);
var results = await connection.QueryAsync<First, Second, int>(command, (a, b) => a.A + b.B, splitOn: "B");

Assert.Equal(3, results.Single());
}

private sealed class First { public int A { get; set; } }
private sealed class Second { public int B { get; set; } }
}
#endif