You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Clean up exceptions, introduce PostgresException in addition to NpgsqlException #897
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.
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.
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.
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?
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.
@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...
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.
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...
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.
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.
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.
changed the title [-]Network issues should trigger NpgsqlException[/-][+]Clean up exceptions, introduce PostgresException in addition to NpgsqlException[/+]on May 5, 2016
@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;"
@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.
Activity
roji commentedon Dec 7, 2015
Interesting... The MSDN docs for SqlException say that:
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.
MattD-Hubble commentedon Dec 8, 2015
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.
roji commentedon Dec 8, 2015
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?
Emill commentedon Dec 8, 2015
I think IOException is preferred...
MattD-Hubble commentedon Dec 9, 2015
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.
roji commentedon Dec 9, 2015
@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...
MattD-Hubble commentedon Dec 31, 2015
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.
roji commentedon Dec 31, 2015
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...
MattD-Hubble commentedon Dec 31, 2015
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.
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."
roji commentedon Jan 1, 2016
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.
Emill commentedon Jan 1, 2016
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.
roji commentedon Jan 1, 2016
@Emill good idea, although everything should assignable to NpgsqlException. So maybe:
Sounds good?
Emill commentedon Jan 1, 2016
Yes!
[-]IOException instead of DbException when connection lost[/-][+]Frontend/backend exception redo[/+]roji commentedon Mar 29, 2016
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.
oguzyildizz commentedon Apr 5, 2016
Thanks @roji !
[-]Frontend/backend exception redo[/-][+]Network issues should trigger NpgsqlException[/+]roji commentedon May 1, 2016
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.
[-]Network issues should trigger NpgsqlException[/-][+]Clean up exceptions, introduce PostgresException in addition to NpgsqlException[/+]swjain23 commentedon Jun 16, 2020
@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':
YohDeadfall commentedon Jun 16, 2020
If
ErrorCodeisn'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).
swjain23 commentedon Jun 16, 2020
@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.