-
Notifications
You must be signed in to change notification settings - Fork 237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
connection_loss thrown on protocol errors #686
Comments
I looked at other exceptions that don't expose an error code:
|
Okay, I'll look into exposing sqlstate on more exception classes. Thanks for digging into this. Haven't had time to respond, but I'll incorporate it one way or another. |
See #686. This is not a full implementatino of what is suggested there. But it will enable a program to avoid the problem of catching a `broken_connection` and retrying ad infniitum.
Doing #687 as a first step in addressing the concrete hazard, by enabling a broken application to detect that a particular (For the record, nobody recommended parsing error messages. I totally agree that parsing error messages would be a terrible suggestion, but that is just not a thing that happened.) |
Does it break the connection in the sense that a reconnect is needed? |
Yes, a That's also one of the reasons by the way why I don't think it'd work to make I had a look into what it would take to add In most cases, libpqxx would choose an sqlstate code, but it can't necessarily promise that it'll be a good choice. For example, when unable to connect to the server, how would libpqxx know whether to use 08001 (SQL-client unable to establish SQL-connection) or 08004 (SQL-server rejected establishment of SQL-connection)? What if it used 08000 and maybe later versions made the code more specific in some scenarios? It seems quite arbitrary and subject to change, which still leaves you with a messy decision matrix for drawing useful conclusions from the sqlstate. So for now I'm leaning in the direction of keeping the existing approach: if you've got a useful distinction between errors, I try to give them separate classes, and inheritance keeps the change compatible with older code. When that doesn't work, we discuss and look for a better solution. And from a practical perspective, I think "my app needs to know this distinction" is a better driver for that discussion than "which is the more appropriate sqlstate code." |
It is a little surprising that a failure to prepare kills a connection. Thanks! |
Yes, it's absolutely surprising! But not much I can do about it. Yes, a Best practices? The usual stuff I suppose... Don't retry the same thing forever. Report exceptions somewhere unless you know very precisely what caused them and why it's irrelevant, so a human can see problem patterns happening. Don't over-complicate error handling, especially since error handling code is often hard to test thoroughly. Accept that anything can fail, including your own code, so don't try to fix every possible problem. And of course if you want to reconnect, you'll have to have your connection string again. |
See #686. This is not a full implementatino of what is suggested there. But it will enable a program to avoid the problem of catching a `broken_connection` and retrying ad infniitum.
As a follow-up to #287:
I am getting the same message, and it took me to that discussion
And herein lies the problem:
pqxx::broken_connection
does not expose the error code. If it did, I could check it and decide what to do based on that.A reasonable assumption that a
pqxx::broken_connection
just indicates a connection loss, could (and has!) lead one to write code that catches the exception and attempts to reconnect and resubmit, resulting a loop that cannot be resolved.Parsing an error message is a terrible advice. That's what error codes are for!
The simple solution is to attach an error code to the exception.
In fact, any exception generated from a condition that has an error code (as per https://www.postgresql.org/docs/current/errcodes-appendix.html), should expose it.
Looking at
result.cxx
I see that the problem is with all of class08
, as well as with codes53300
and42501
.There are several options to deal with it:
connection_error
undersql_error
.failure_with_code
orsql_failure
as a parent of bothconnection_error
andsql_error
, and movesqlstate()
to it.sqlstate()
toconnection_error
.I personally prefer #1.
Thank you!
The text was updated successfully, but these errors were encountered: