Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

close readers *before* recycling - required for npgsql #74

Merged
merged 1 commit into from
Nov 15, 2023
Merged
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
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
Loading