From: Kris Jurka <books@ejurka.com>
To: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: Donald Fraser <postgres@kiwi-fraser.net>
Cc: [JDBC] <pgsql-jdbc@postgresql.org>
Subject: Re: Invalid PSQLState used in connection classes
Date: Sat, 19 Mar 2011 19:47:31 -0400 (EDT)
Message-ID: <alpine.BSO.2.00.1103191936050.28447@leary.csoft.net> (raw)
In-Reply-To: <4D760999020000250003B603@gw.wicourts.gov>
References: <CD183140E21E4179B9F1F5DD77EB4E2F@DEVELOP1>
<4D760999020000250003B603@gw.wicourts.gov>
On Tue, 8 Mar 2011, Kevin Grittner wrote:
> "Donald Fraser" <postgres@kiwi-fraser.net> wrote:
>
>> However if I have a broken network between my client and the
>> server, such that there is no route to the server, I can get a
>> ConnectException and I have not established, in any way, a
>> connection to the PostgreSQL server.
>
> 08001 - SQLCLIENT UNABLE TO ESTABLISH SQLCONNECTION
>
> Sounds better for that sort of failure.
>
Sounds reasonable to me. Looking at the error codes used during startup,
I think we should also make some other changes at the same time.
CONNECTION_UNABLE_TO_CONNECT -> PROTOCOL_VIOLATION
CONNECTION_FAILURE -> PROTOCOL_VIOLATION
when the driver receives an unknown message.
CONNECTION_FAILURE -> CONNECTION_REJECTED
when the server doesn't support SSL, but we need a SSL connection. I
think CONNECTION_FAILURE should only be used when something has gone wrong
after a connection has been established.
Patch attached.
Kris Jurka
Attachments:connection-sqlstates.patch (text/plain, 12.7K) download
Index: org/postgresql/core/v2/ConnectionFactoryImpl.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v2/ConnectionFactoryImpl.java,v
retrieving revision 1.20
diff -c -r1.20 ConnectionFactoryImpl.java
*** org/postgresql/core/v2/ConnectionFactoryImpl.java 25 Dec 2010 07:07:44 -0000 1.20
--- org/postgresql/core/v2/ConnectionFactoryImpl.java 19 Mar 2011 23:34:44 -0000
***************
*** 101,107 ****
// Added by Peter Mount <peter@retep.org.uk>
// ConnectException is thrown when the connection cannot be made.
// we trap this an return a more meaningful message for the end user
! throw new PSQLException (GT.tr("Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."), PSQLState.CONNECTION_REJECTED, cex);
}
catch (IOException ioe)
{
--- 101,107 ----
// Added by Peter Mount <peter@retep.org.uk>
// ConnectException is thrown when the connection cannot be made.
// we trap this an return a more meaningful message for the end user
! throw new PSQLException (GT.tr("Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."), PSQLState.CONNECTION_UNABLE_TO_CONNECT, cex);
}
catch (IOException ioe)
{
***************
*** 155,161 ****
// Server doesn't even know about the SSL handshake protocol
if (requireSSL)
! throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_FAILURE);
// We have to reconnect to continue.
pgStream.close();
--- 155,161 ----
// Server doesn't even know about the SSL handshake protocol
if (requireSSL)
! throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_REJECTED);
// We have to reconnect to continue.
pgStream.close();
***************
*** 167,173 ****
// Server does not support ssl
if (requireSSL)
! throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_FAILURE);
return pgStream;
--- 167,173 ----
// Server does not support ssl
if (requireSSL)
! throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_REJECTED);
return pgStream;
***************
*** 180,186 ****
return pgStream;
default:
! throw new PSQLException(GT.tr("An error occured while setting up the SSL connection."), PSQLState.CONNECTION_FAILURE);
}
}
--- 180,186 ----
return pgStream;
default:
! throw new PSQLException(GT.tr("An error occured while setting up the SSL connection."), PSQLState.PROTOCOL_VIOLATION);
}
}
***************
*** 319,325 ****
break;
default:
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT);
}
}
}
--- 319,325 ----
break;
default:
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION);
}
}
}
***************
*** 357,363 ****
break;
default:
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT);
}
}
}
--- 357,363 ----
break;
default:
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION);
}
}
}
Index: org/postgresql/core/v3/ConnectionFactoryImpl.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v
retrieving revision 1.23
diff -c -r1.23 ConnectionFactoryImpl.java
*** org/postgresql/core/v3/ConnectionFactoryImpl.java 25 Dec 2010 07:07:44 -0000 1.23
--- org/postgresql/core/v3/ConnectionFactoryImpl.java 19 Mar 2011 23:34:44 -0000
***************
*** 135,141 ****
// Added by Peter Mount <peter@retep.org.uk>
// ConnectException is thrown when the connection cannot be made.
// we trap this an return a more meaningful message for the end user
! throw new PSQLException (GT.tr("Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."), PSQLState.CONNECTION_REJECTED, cex);
}
catch (IOException ioe)
{
--- 135,141 ----
// Added by Peter Mount <peter@retep.org.uk>
// ConnectException is thrown when the connection cannot be made.
// we trap this an return a more meaningful message for the end user
! throw new PSQLException (GT.tr("Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."), PSQLState.CONNECTION_UNABLE_TO_CONNECT, cex);
}
catch (IOException ioe)
{
***************
*** 187,193 ****
// Server doesn't even know about the SSL handshake protocol
if (requireSSL)
! throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_FAILURE);
// We have to reconnect to continue.
pgStream.close();
--- 187,193 ----
// Server doesn't even know about the SSL handshake protocol
if (requireSSL)
! throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_REJECTED);
// We have to reconnect to continue.
pgStream.close();
***************
*** 199,205 ****
// Server does not support ssl
if (requireSSL)
! throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_FAILURE);
return pgStream;
--- 199,205 ----
// Server does not support ssl
if (requireSSL)
! throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_REJECTED);
return pgStream;
***************
*** 212,218 ****
return pgStream;
default:
! throw new PSQLException(GT.tr("An error occured while setting up the SSL connection."), PSQLState.CONNECTION_FAILURE);
}
}
--- 212,218 ----
return pgStream;
default:
! throw new PSQLException(GT.tr("An error occured while setting up the SSL connection."), PSQLState.PROTOCOL_VIOLATION);
}
}
***************
*** 406,412 ****
break;
default:
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT);
}
}
}
--- 406,412 ----
break;
default:
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION);
}
}
}
***************
*** 449,455 ****
// BackendKeyData
int l_msgLen = pgStream.ReceiveInteger4();
if (l_msgLen != 12)
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT);
int pid = pgStream.ReceiveInteger4();
int ckey = pgStream.ReceiveInteger4();
--- 449,455 ----
// BackendKeyData
int l_msgLen = pgStream.ReceiveInteger4();
if (l_msgLen != 12)
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION);
int pid = pgStream.ReceiveInteger4();
int ckey = pgStream.ReceiveInteger4();
***************
*** 495,501 ****
else if (name.equals("client_encoding"))
{
if (!value.equals("UNICODE"))
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT);
pgStream.setEncoding(Encoding.getDatabaseEncoding("UNICODE"));
}
else if (name.equals("standard_conforming_strings"))
--- 495,501 ----
else if (name.equals("client_encoding"))
{
if (!value.equals("UNICODE"))
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION);
pgStream.setEncoding(Encoding.getDatabaseEncoding("UNICODE"));
}
else if (name.equals("standard_conforming_strings"))
***************
*** 505,511 ****
else if (value.equals("off"))
protoConnection.setStandardConformingStrings(false);
else
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT);
}
break;
--- 505,511 ----
else if (value.equals("off"))
protoConnection.setStandardConformingStrings(false);
else
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION);
}
break;
***************
*** 513,519 ****
default:
if (logger.logDebug())
logger.debug("invalid message type=" + (char)beresp);
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT);
}
}
}
--- 513,519 ----
default:
if (logger.logDebug())
logger.debug("invalid message type=" + (char)beresp);
! throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION);
}
}
}
Index: org/postgresql/util/PSQLState.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/util/PSQLState.java,v
retrieving revision 1.13
diff -c -r1.13 PSQLState.java
*** org/postgresql/util/PSQLState.java 8 Jan 2008 06:56:31 -0000 1.13
--- org/postgresql/util/PSQLState.java 19 Mar 2011 23:34:44 -0000
***************
*** 37,48 ****
--- 37,70 ----
public final static PSQLState INVALID_PARAMETER_TYPE = new PSQLState("07006");
+ /**+ * We could establish a connection with the server for unknown reasons.+ * Could be a network problem.+ */
public final static PSQLState CONNECTION_UNABLE_TO_CONNECT = new PSQLState("08001");
+
public final static PSQLState CONNECTION_DOES_NOT_EXIST = new PSQLState("08003");
+ + /**+ * The server rejected our connection attempt. Usually an authentication+ * failure, but could be a configuration error like asking for a SSL+ * connection with a server that wasn't built with SSL support.+ */
public final static PSQLState CONNECTION_REJECTED = new PSQLState("08004");
+ + /**+ * After a connection has been established, it went bad.+ */
public final static PSQLState CONNECTION_FAILURE = new PSQLState("08006");
public final static PSQLState CONNECTION_FAILURE_DURING_TRANSACTION = new PSQLState("08007");
+ + /**+ * The server sent us a response the driver was not prepared for and+ * is either bizarre datastream corruption, a driver bug, or+ * a protocol violation on the server's part.+ */
public final static PSQLState PROTOCOL_VIOLATION = new PSQLState("08P01");
+
public final static PSQLState COMMUNICATION_ERROR = new PSQLState("08S01");
public final static PSQLState NOT_IMPLEMENTED = new PSQLState("0A000");
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: books@ejurka.com, Kevin.Grittner@wicourts.gov, postgres@kiwi-fraser.net
Subject: Re: Invalid PSQLState used in connection classes
In-Reply-To: <alpine.BSO.2.00.1103191936050.28447@leary.csoft.net>
References: <CD183140E21E4179B9F1F5DD77EB4E2F@DEVELOP1> <4D760999020000250003B603@gw.wicourts.gov> <alpine.BSO.2.00.1103191936050.28447@leary.csoft.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox