Skip to content

Commit

Permalink
consistent changes with DapperLib/Dapper#2121
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Oct 8, 2024
1 parent 0b5f6d7 commit 60c12e1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/Dapper.AOT/Internal/AsyncCommandState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const int
[MemberNotNull(nameof(Command))]
public Task<DbDataReader> ExecuteReaderAsync(DbCommand command, CommandBehavior flags, CancellationToken cancellationToken)
{
flags &= ~(CommandBehavior.SingleResult | CommandBehavior.SingleRow); // correctness; these can mask trailing error data

var pending = OnBeforeExecuteAsync(command, cancellationToken);
return pending.IsCompletedSuccessfully() ? command.ExecuteReaderAsync(flags, cancellationToken)
: Awaited(pending, command, flags, cancellationToken);
Expand Down Expand Up @@ -113,6 +115,19 @@ static async Task<int> Awaited(Task pending, DbCommand command, CancellationToke
}
}

public void CancelCommand()
{
try
{
Command?.Cancel();
}
catch (Exception ex)
{
// do not lose any existing exception
Debug.WriteLine(ex.Message);
}
}

public virtual ValueTask DisposeAsync()
{
var cmd = Command;
Expand Down
5 changes: 5 additions & 0 deletions src/Dapper.AOT/Internal/AsyncQueryState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public override ValueTask DisposeAsync()
Return();
if (Reader is not null)
{
CancelCommand();

#if NETCOREAPP3_1_OR_GREATER
var pending = Reader.DisposeAsync();
if (pending.IsCompletedSuccessfully)
Expand All @@ -98,11 +100,14 @@ public override ValueTask DisposeAsync()
}
return base.DisposeAsync();
}

#if NETCOREAPP3_1_OR_GREATER
private async ValueTask DisposeAsync(ValueTask pending)
{
await pending;
await base.DisposeAsync();
}
#endif

public override void Dispose()
{
Expand Down
18 changes: 17 additions & 1 deletion src/Dapper.AOT/Internal/SyncCommandState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Data;
using System;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -28,6 +29,8 @@ const int
[MemberNotNull(nameof(Command))]
public DbDataReader ExecuteReader(DbCommand command, CommandBehavior flags)
{
flags &= (CommandBehavior.SingleResult | CommandBehavior.SingleRow); // correctness; these can mask trailing error data

OnBeforeExecute(command);
return command.ExecuteReader(flags);
}
Expand Down Expand Up @@ -58,6 +61,19 @@ public int ExecuteNonQuery(DbCommand command)
return command.ExecuteNonQuery();
}

public void CancelCommand()
{
try
{
Command?.Cancel();
}
catch (Exception ex)
{
// do not lose any existing exception
Debug.WriteLine(ex.Message);
}
}

public void Dispose()
{
var cmd = Command;
Expand Down
6 changes: 5 additions & 1 deletion src/Dapper.AOT/Internal/SyncQueryState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public ValueTask DisposeAsync()
public void Dispose()
{
Return();
Reader?.Dispose();
if (Reader is not null)
{
commandState.CancelCommand();
Reader.Dispose();
}
commandState.Dispose();
}

Expand Down
2 changes: 1 addition & 1 deletion test/Dapper.AOT.Test/Integration/BatchPostgresql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Dapper.AOT.Test.Integration;
[Collection(SharedPostgresqlClient.Collection)]
public class BatchPostgresql
{
private PostgresqlFixture _fixture;
private readonly PostgresqlFixture _fixture;

public BatchPostgresql(PostgresqlFixture fixture)
{
Expand Down

0 comments on commit 60c12e1

Please sign in to comment.