Npgsqldatareader not disposing if error happens on multiple statements and not executed statement has syntax error
See original GitHub issueSteps to reproduce
Create the transaction object on connection create command and datareader by using block Create the command with multiple statements (4 statements) and make last query with syntax error Read first two result set and while reading third result pass invalid ordinal position on getfieldvalue<T> to raise error
call transaction.rollback will get the “A command is already in progress” error
Sample Code
await using(var cmd = Connection.CreateCommand())
{
cmd.CommandText = "Correct Query 1; Correct Query 2";Correct Query 3;Query with syntax Error;";
await using(var dr = await cmd.ExecuteReaderAsync())
{
await dr.ReadAsync();
commonSettings = _CommandSettingsQuery.Read(dr);
await dr.NextResultAsync();
await dr.ReadAsync();
mobileSettings = _MobileSettingsQuery.Read(dr);
await dr.NextResultAsync();
await dr.ReadAsync();
otpFor = _OtpForQuery.Read(dr); // While reading, error will throw as passing invalid ordinal or by truncating table
existingUsers = new List<UserDto>();
if(await dr.NextResultAsync())
{
while(await dr.ReadAsync())
{
existingUsers.Add(_UserQuery.ReadUser(dr));
}
}
}
}
The issue
The Datareader should have disposed and rollback should be called without the error
If you are seeing an exception, include the full exceptions details (message and stack trace). The log shows the follows
Exception caught while disposing a reader
Npgsql.PostgresException (0x80004005): 42601: syntax error at or near "$3"
at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|194_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlDataReader.Consume(Boolean async)
at Npgsql.NpgsqlDataReader.Close(Boolean connectionClosing, Boolean async, Boolean isDisposing)
at Npgsql.NpgsqlDataReader.<DisposeAsync>g__DisposeAsyncCore|67_0()
Exception data:
Severity: ERROR
SqlState: 42601
MessageText: syntax error at or near "$3"
Position: 93
File: scan.l
Line: 1176
Routine: scanner_yyerror
Further technical details
Npgsql version: 5.0.3 PostgreSQL version: 13 Operating system: Ubuntu .Net Version : .Net 5
Other details about my project setup: Asp.Net core Web Application
Is this a expected behaviour. if yes then is there anyway to rollback transaction Thank you
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
You actually did help me to find a few more bugs with
KeepAlive
, so nevermind 😃 You’re welcome.Hi , It was my mistake and i was using was 5.0.1.1 after upgrading to 5.0.3 works as expected “Also note, that this kind of error was fixed with 5.0.2 release.” this line helped me to recheck all projects dependencies
Sorry consumed your precious time Thanks a lot