question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Npgsqldatareader not disposing if error happens on multiple statements and not executed statement has syntax error

See original GitHub issue

Steps 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:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vonzshikcommented, Feb 7, 2021

You actually did help me to find a few more bugs with KeepAlive, so nevermind 😃 You’re welcome.

1reaction
MohanVijayakumarcommented, Feb 7, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Npgsql Exception- syntax error at or near "\"
I am trying to execute postgresql query (\list) to get the list of databases using npsql. string postgresQuery = @"\list"; ICollection<Schema> ...
Read more >
libpq - 34.3. Command Execution Functions
The command string can include multiple SQL commands (separated by semicolons). ... NULL is returned if the PGresult is not an error or...
Read more >
Npgsql Basic Usage
The common example for this is a database transaction, where multiple commands need to be executed within the same transaction, on the same...
Read more >
Q&A
Hello,. I am getting this error while I am running my code.(42601 : syntax error at or near "'Microsoft.ACE.OLEDB.12.0'"/).
Read more >
Postgre SQL errors – common codes and messages
The error message “syntax error at or near 'grant'” is one of the most common PostgreSQL database errors. However, it can easily be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found