Skip to content

Commit 60c12e1

Browse files
committed
consistent changes with DapperLib/Dapper#2121
1 parent 0b5f6d7 commit 60c12e1

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

src/Dapper.AOT/Internal/AsyncCommandState.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const int
3737
[MemberNotNull(nameof(Command))]
3838
public Task<DbDataReader> ExecuteReaderAsync(DbCommand command, CommandBehavior flags, CancellationToken cancellationToken)
3939
{
40+
flags &= ~(CommandBehavior.SingleResult | CommandBehavior.SingleRow); // correctness; these can mask trailing error data
41+
4042
var pending = OnBeforeExecuteAsync(command, cancellationToken);
4143
return pending.IsCompletedSuccessfully() ? command.ExecuteReaderAsync(flags, cancellationToken)
4244
: Awaited(pending, command, flags, cancellationToken);
@@ -113,6 +115,19 @@ static async Task<int> Awaited(Task pending, DbCommand command, CancellationToke
113115
}
114116
}
115117

118+
public void CancelCommand()
119+
{
120+
try
121+
{
122+
Command?.Cancel();
123+
}
124+
catch (Exception ex)
125+
{
126+
// do not lose any existing exception
127+
Debug.WriteLine(ex.Message);
128+
}
129+
}
130+
116131
public virtual ValueTask DisposeAsync()
117132
{
118133
var cmd = Command;

src/Dapper.AOT/Internal/AsyncQueryState.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public override ValueTask DisposeAsync()
8282
Return();
8383
if (Reader is not null)
8484
{
85+
CancelCommand();
86+
8587
#if NETCOREAPP3_1_OR_GREATER
8688
var pending = Reader.DisposeAsync();
8789
if (pending.IsCompletedSuccessfully)
@@ -98,11 +100,14 @@ public override ValueTask DisposeAsync()
98100
}
99101
return base.DisposeAsync();
100102
}
103+
104+
#if NETCOREAPP3_1_OR_GREATER
101105
private async ValueTask DisposeAsync(ValueTask pending)
102106
{
103107
await pending;
104108
await base.DisposeAsync();
105109
}
110+
#endif
106111

107112
public override void Dispose()
108113
{

src/Dapper.AOT/Internal/SyncCommandState.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Data;
1+
using System;
2+
using System.Data;
23
using System.Data.Common;
34
using System.Diagnostics;
45
using System.Diagnostics.CodeAnalysis;
@@ -28,6 +29,8 @@ const int
2829
[MemberNotNull(nameof(Command))]
2930
public DbDataReader ExecuteReader(DbCommand command, CommandBehavior flags)
3031
{
32+
flags &= (CommandBehavior.SingleResult | CommandBehavior.SingleRow); // correctness; these can mask trailing error data
33+
3134
OnBeforeExecute(command);
3235
return command.ExecuteReader(flags);
3336
}
@@ -58,6 +61,19 @@ public int ExecuteNonQuery(DbCommand command)
5861
return command.ExecuteNonQuery();
5962
}
6063

64+
public void CancelCommand()
65+
{
66+
try
67+
{
68+
Command?.Cancel();
69+
}
70+
catch (Exception ex)
71+
{
72+
// do not lose any existing exception
73+
Debug.WriteLine(ex.Message);
74+
}
75+
}
76+
6177
public void Dispose()
6278
{
6379
var cmd = Command;

src/Dapper.AOT/Internal/SyncQueryState.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public ValueTask DisposeAsync()
3131
public void Dispose()
3232
{
3333
Return();
34-
Reader?.Dispose();
34+
if (Reader is not null)
35+
{
36+
commandState.CancelCommand();
37+
Reader.Dispose();
38+
}
3539
commandState.Dispose();
3640
}
3741

test/Dapper.AOT.Test/Integration/BatchPostgresql.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Dapper.AOT.Test.Integration;
77
[Collection(SharedPostgresqlClient.Collection)]
88
public class BatchPostgresql
99
{
10-
private PostgresqlFixture _fixture;
10+
private readonly PostgresqlFixture _fixture;
1111

1212
public BatchPostgresql(PostgresqlFixture fixture)
1313
{

0 commit comments

Comments
 (0)