Skip to content

Commit

Permalink
close *before* recycling - required for npgsql (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell authored Nov 15, 2023
1 parent ceadcea commit 0ea03d8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/Dapper.AOT/CommandT.Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public List<TRow> QueryBuffered<TRow>(TArgs args, [DapperAot] RowFactory<TRow>?

// consume entire results (avoid unobserved TDS error messages)
while (state.Reader.NextResult()) { }
PostProcessAndRecycle(ref state, args, state.Reader.RecordsAffected);
PostProcessAndRecycle(ref state, args, state.Reader.CloseAndCapture());
return results;
}
finally
Expand Down Expand Up @@ -90,7 +90,7 @@ public async Task<List<TRow>> QueryBufferedAsync<TRow>(TArgs args, [DapperAot] R

// consume entire results (avoid unobserved TDS error messages)
while (await state.Reader.NextResultAsync(cancellationToken)) { }
PostProcessAndRecycle(state, args, state.Reader.RecordsAffected);
PostProcessAndRecycle(state, args, await state.Reader.CloseAndCaptureAsync());
return results;
}
finally
Expand Down Expand Up @@ -122,7 +122,7 @@ public async IAsyncEnumerable<TRow> QueryUnbufferedAsync<TRow>(TArgs args, [Dapp
}
// consume entire results (avoid unobserved TDS error messages)
while (await state.Reader.NextResultAsync(cancellationToken)) { }
PostProcessAndRecycle(state, args, state.Reader.RecordsAffected);
PostProcessAndRecycle(state, args, await state.Reader.CloseAndCaptureAsync());
}
finally
{
Expand Down Expand Up @@ -152,7 +152,7 @@ public IEnumerable<TRow> QueryUnbuffered<TRow>(TArgs args, [DapperAot] RowFactor
}
// consume entire results (avoid unobserved TDS error messages)
while (state.Reader.NextResult()) { }
PostProcessAndRecycle(ref state, args, state.Reader.RecordsAffected);
PostProcessAndRecycle(ref state, args, state.Reader.CloseAndCapture());
}
finally
{
Expand Down Expand Up @@ -198,7 +198,7 @@ static CommandBehavior SingleFlags(OneRowFlags flags)

// consume entire results (avoid unobserved TDS error messages)
while (state.Reader.NextResult()) { }
PostProcessAndRecycle(ref state, args, state.Reader.RecordsAffected);
PostProcessAndRecycle(ref state, args, state.Reader.CloseAndCapture());
return result;
}
finally
Expand Down Expand Up @@ -241,7 +241,7 @@ static CommandBehavior SingleFlags(OneRowFlags flags)

// consume entire results (avoid unobserved TDS error messages)
while (await state.Reader.NextResultAsync(cancellationToken)) { }
PostProcessAndRecycle(state, args, state.Reader.RecordsAffected);
PostProcessAndRecycle(state, args, await state.Reader.CloseAndCaptureAsync());
return result;
}
finally
Expand Down
28 changes: 28 additions & 0 deletions src/Dapper.AOT/Internal/CommandUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Data.Common;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -54,6 +55,33 @@ internal static bool IsCompletedSuccessfully(this Task task)
#endif
}

[MethodImpl(AggressiveOptions)]
internal static int CloseAndCapture(this DbDataReader? reader)
{
if (reader is null) return -1;
var count = reader.RecordsAffected;
reader.Close();
return count;
}

[MethodImpl(AggressiveOptions)]
internal static ValueTask<int> CloseAndCaptureAsync(this DbDataReader? reader)
{
#if NETCOREAPP3_1_OR_GREATER
if (reader is null) return new(-1);
var count = reader.RecordsAffected;
var pending = reader.CloseAsync();
return pending.IsCompletedSuccessfully ? new(count) : Deferred(pending, count);
static async ValueTask<int> Deferred(Task pending, int count)
{
await pending;
return count;
}
#else
return new(CloseAndCapture(reader));
#endif
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowNull() => throw new ArgumentNullException("value");

Expand Down
4 changes: 2 additions & 2 deletions src/Dapper.AOT/WrappedDbDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public sealed override void Close()
var state = this.state; // snapshot
if (state is not null)
{
commandFactory.PostProcessObject(new(state.Command!), args, state.Reader?.RecordsAffected ?? -1);
commandFactory.PostProcessObject(new(state.Command!), args, state.Reader.CloseAndCapture());
if (commandFactory.TryRecycle(state.Command!))
{
state.Command = null;
Expand Down Expand Up @@ -229,7 +229,7 @@ private async Task CloseAsyncImpl()
var state = this.state; // snapshot
if (state is not null)
{
commandFactory.PostProcessObject(new(state.Command!), args, state.Reader?.RecordsAffected ?? -1);
commandFactory.PostProcessObject(new(state.Command!), args, await state.Reader.CloseAndCaptureAsync());
if (commandFactory.TryRecycle(state.Command!))
{
state.Command = null;
Expand Down

0 comments on commit 0ea03d8

Please sign in to comment.