Skip to content

Clean up exceptions, introduce PostgresException in addition to NpgsqlException #897

Closed

Description

@MattD-Hubble

When I try and test our software's reconnection logic with version 3.0.3 I'm seeing an IOException thrown instead of a DbException subclass. We currently rely on intercepting DbExceptions and examining them to see if we should reconnect.

The initial scenario I test for reconnecting is to let the software create a connection to a local database server, restart the service, then try and use the connection. In SQL Server I get a SqlException ("A transport-level error has occurred...") thrown but with Npgsql I'm getting IOException ("Unable to write data to the transport connection...").
Based on #330 I was hoping to see this change from IOException to NpgsqlException after upgrading Npgsql from version 2.2.5 to 3.0.3 but it hasn't.

exception npgsql.txt
exception sqlserver.txt

Activity

  1. roji commented on Dec 7, 2015

    @roji
    Member

    Interesting... The MSDN docs for SqlException say that:

    This class is created whenever the .NET Framework Data Provider for SQL Server encounters an error generated from the server. (Client side errors are thrown as standard common language runtime exceptions.) SqlException always contains at least one instance of SqlError.
    

    In other words, network errors and the like should not trigger an SqlException/NpgsqlException. One reason this makes sense is that there's no info to populate them with (e.g. NpgsqlException's Code property). This is why in #330 we switched to simply letting network errors propagate without wrapping them in NpgsqlExceptions as was the case in 2.x.

    So the SqlException error you're seeing seems to contradict Microsoft's own documentation. Looking at your stack trace I can see you're trying to connect via shared memory, I wonder what the behavior would be like when connecting via TCP/IP...

    In any case, it's unlikely that we'll change this behavior... To catch connection issues, you should narrow your try/catch block to your NpgsqlConnection.Open specifically. Please let me know if you have further thoughts on this.

  2. MattD-Hubble commented on Dec 8, 2015

    @MattD-Hubble
    Author

    That's interesting thanks. In 2.2.5 we were seeing non-DbException and I'd misread #330 to say that these would be wrapped in version 3.
    Reading the SqlException spec it seems ambiguous to me about how communication errors should be handled with a later paragraph highlighting OutOfMemoryException, ThreadAbortException and input validation as types of framework errors that could be expected.

    My practical problem with the current implementation is that all of the other database drivers we use throw implementations of DbException for communication errors which allow us to capture transient problems (as opposed things like null connection strings) by catching DbException.
    Although Npgsql would have to provide an error code, ODBC uses SqlStates (https://msdn.microsoft.com/en-us/library/ms714687.aspx) for connection errors, i.e. those starting with "08". It's one of these (08003) that I get after restarting my SQL Server service.

  3. roji commented on Dec 8, 2015

    @roji
    Member

    Thanks for the info - that's very interesting indeed. Do the SqlClient exceptions contain the actual low-level exception (e.g. IOException) as an inner exception?

    I can definitely see how wrapping I/O exceptions in NpgsqlException could be useful for users, what other database drivers have you seen with this behavior? Even if this would reverse the direction taken in 3.0 (#330), maybe it makes sense to do this for 3.1. @Emill, @damageboy, any thoughts?

  4. Emill commented on Dec 8, 2015

    @Emill
    Contributor

    I think IOException is preferred...

  5. MattD-Hubble commented on Dec 9, 2015

    @MattD-Hubble
    Author

    The exception from SQL Server has a Win32Exception as its inner exception, which provides:
    ErrorCode: -2147467259
    HResult: -2147467259
    HelpLink: null
    InnerException: null
    Message: "No process is on the other end of the pipe"
    NativeErrorCode: 233
    Source: null
    StackTrace: null
    We've got a lookup table which converts the native error code 233 (exposed through the SqlException) to the the SQL state 08003.

    Apart from the SQL Server client the other drivers we've tested our reconnection with (and therefore throw DbException) are JDBC drivers wrapped up in a .NET wrapper; so probably not a precedent here.
    A quick test I've done was with Oracle's JDBC driver; it threw a SQLRecoverableException if the network connecton is down when a query is made. Our wrapper wraps up java.sql.SQLException into a DbException implementation for the .NET code to handle.
    I've had a go at getting OracleClient and ODBC drivers up and running to see what they do but there's a bit of Oracle magic I'm missing.

    If you'd rather not change the current behaviour we can always look into forking or wrapping Npgsql but wanted to have this discussion.

  6. roji commented on Dec 9, 2015

    @roji
    Member

    @Emill, any specific reason? If other ADO.NET providers wrap IOException in DbException it may be the "standard" thing to do (need more info here). I can also see the advantage for users for simply doing try/catch over DbException rather than having to worry about IOException which could also come from non-database code...

  7. MattD-Hubble commented on Dec 31, 2015

    @MattD-Hubble
    Author

    Hi, has there been any more discussion on this?
    I just to make sure that this case hasn't been forgotten, and would like to get a feel for which way it's likely to go.

  8. roji commented on Dec 31, 2015

    @roji
    Member

    Sorry, I'm rather taken up with other things at the moment...

    I admit I'm leaning a bit more towards wrapping in NpgsqlException like you originally requested. The best way forward here is to simply have a view of how other .NET providers handle it, which you've started doing. Any feedback on how mysql/oracle and others would help here, because we're ultimately aiming to do the compatible/standard thing...

  9. added this to the milestone on Dec 31, 2015
  10. MattD-Hubble commented on Dec 31, 2015

    @MattD-Hubble
    Author

    Thanks for the guidance and no worries with the delay.

    I've run a quick test with Oracle and MySql, in both cases with this simple scenario.

    • Create and open a connection.
    • Create a command using that connection and a Select statement.
    • Disconnect the network (for the error case)
    • ExecuteReader on the command and try to output the rows returned.

    Oracle.ManagedDataAccess v12.1.2400
    Oracle.ManagedDataAccess.Client.OracleException, a subclass of DbException.
    Message = "Oracle error ORA-12571 encountered"

    MySql.Data v6.9.8
    MySql.Data.MySqlClient.MySqlException, a subclass of DbException
    Message = "Fatal error encountered during command execution."

  11. roji commented on Jan 1, 2016

    @roji
    Member

    Thanks for doing these tests @MattD-Hubble, that seems pretty conclusive to me...

    @Emill, unless you have a concrete objection it seems that for 3.1 we should catch exceptions (e.g. IO) and throw them as NpgsqlExceptions. The original exception should be kept as the InnerException.

  12. Emill commented on Jan 1, 2016

    @Emill
    Contributor

    Hm, maybe that might be a good idea. However, there should be two different kinds of exceptions: one for PostgreSQL errors (such as sql parsing/execution errors etc.) and one for network errors.

  13. roji commented on Jan 1, 2016

    @roji
    Member

    @Emill good idea, although everything should assignable to NpgsqlException. So maybe:

    • For non-PostgreSQL errors (e.g. IO), we throw an NpgsqlException with the InnerException field set
    • For PostgreSQL errors, we throw a PostgresException (which extends NpgsqlException). This class will have all the PostgreSQL fields (e.g. Code)

    Sounds good?

  14. Emill commented on Jan 1, 2016

    @Emill
    Contributor

    Yes!

  15. changed the title [-]IOException instead of DbException when connection lost[/-] [+]Frontend/backend exception redo[/+] on Mar 29, 2016
  16. roji commented on Mar 29, 2016

    @roji
    Member

    Another important note: it is probably useful to add error codes for frontend exceptions, to allow programs to check for certain errors without parsing the message text (which may also be localized one day). This shouldn't be the same error-code-space as the backend exception.

  17. oguzyildizz commented on Apr 5, 2016

    @oguzyildizz

    Thanks @roji !

  18. changed the title [-]Frontend/backend exception redo[/-] [+]Network issues should trigger NpgsqlException[/+] on May 1, 2016
  19. roji commented on May 1, 2016

    @roji
    Member

    Looking at this again, it seems that the SQL standard defines some SQLSTATE codes that are meant to be used for client-side networking errors - see class 08 in http://www.postgresql.org/docs/9.4/static/errcodes-appendix.html. I've also checked the libpq source code and it indeed sets the SQLSTATE when network errors occur.

    So it seems more correct for Npgsql to simply generate an NpgsqlException and set its Code property.

  20. changed the title [-]Network issues should trigger NpgsqlException[/-] [+]Clean up exceptions, introduce PostgresException in addition to NpgsqlException[/+] on May 5, 2016
  21. swjain23 commented on Jun 16, 2020

    @swjain23

    @roji - Hi, we were looking at the NpgSql exception that we get when the SSL is not available "SSL connection requested. No SSL enabled connection from this host is configured.". You mentioned that client side exception should have a code property set so that we do not have to match on the error text. I got this error:
    "Inner exception type: Npgsql.NpgsqlException​
    Message: ​
    SSL connection requested. No SSL enabled connection from this host is configured.​
    HResult : 0x80004005​
    FacilityCode : 0 (000)​
    ErrorCode : 16389 (4005)​"

    Is this error code "16389" unique for this error? Also, is there a list of NpgSql client side errors that we can refer to?
    As I do not see code property being set in the codebase. In NpgSqlCoonector.cs, I see this
    "case 'N':

                        if (SslMode == SslMode.Require)
    
                            throw new NpgsqlException("SSL connection requested. No SSL enabled connection from this host is configured.");
    
                        break;"
    
  22. YohDeadfall commented on Jun 16, 2020

    @YohDeadfall
    Contributor

    If ErrorCode isn't set explicitly, it isn't unique. So you should check the message.

    To track the issue there is #1072, but it's closed due to lack of interest (no one asked about this).

  23. swjain23 commented on Jun 16, 2020

    @swjain23

    @YohDeadfall Thanks for the update. Any plans to fix it? Checking error message is error prone and our code might break if the message is changed in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions