From c9340740951f66b2bb6b38313e74a283d8708173 Mon Sep 17 00:00:00 2001 From: Kris Jurka Date: Thu, 31 Mar 2011 03:06:54 +0000 Subject: [PATCH] Use slightly different SQL State error codes for the different types of connection setup failures to indicate which can be retried and which cannot. Per discussion with Donald Fraser and Kevin Grittner. --- .../core/v2/ConnectionFactoryImpl.java | 14 +++++------ .../core/v3/ConnectionFactoryImpl.java | 20 ++++++++-------- org/postgresql/util/PSQLState.java | 24 ++++++++++++++++++- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/org/postgresql/core/v2/ConnectionFactoryImpl.java b/org/postgresql/core/v2/ConnectionFactoryImpl.java index 6ed2e90f5e..bedd3a7800 100644 --- a/org/postgresql/core/v2/ConnectionFactoryImpl.java +++ b/org/postgresql/core/v2/ConnectionFactoryImpl.java @@ -4,7 +4,7 @@ * Copyright (c) 2004, Open Cloud Limited. * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/core/v2/ConnectionFactoryImpl.java,v 1.13 2007/10/08 01:49:59 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/core/v2/ConnectionFactoryImpl.java,v 1.14 2008/01/08 06:56:27 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -90,7 +90,7 @@ public ProtocolConnection openConnectionImpl(String host, int port, String user, // Added by Peter Mount // 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); + 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) { @@ -144,7 +144,7 @@ private PGStream enableSSL(PGStream pgStream, boolean requireSSL, Properties inf // 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); + throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_REJECTED); // We have to reconnect to continue. pgStream.close(); @@ -156,7 +156,7 @@ private PGStream enableSSL(PGStream pgStream, boolean requireSSL, Properties inf // Server does not support ssl if (requireSSL) - throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_FAILURE); + throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_REJECTED); return pgStream; @@ -169,7 +169,7 @@ private PGStream enableSSL(PGStream pgStream, boolean requireSSL, Properties inf return pgStream; default: - throw new PSQLException(GT.tr("An error occured while setting up the SSL connection."), PSQLState.CONNECTION_FAILURE); + throw new PSQLException(GT.tr("An error occured while setting up the SSL connection."), PSQLState.PROTOCOL_VIOLATION); } } @@ -309,7 +309,7 @@ private void doAuthentication(PGStream pgStream, String user, String password, L break; default: - throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT); + throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION); } } } @@ -347,7 +347,7 @@ private void readStartupMessages(PGStream pgStream, ProtocolConnectionImpl proto break; default: - throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT); + throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION); } } } diff --git a/org/postgresql/core/v3/ConnectionFactoryImpl.java b/org/postgresql/core/v3/ConnectionFactoryImpl.java index 5840910cb6..bbad0a7e6a 100644 --- a/org/postgresql/core/v3/ConnectionFactoryImpl.java +++ b/org/postgresql/core/v3/ConnectionFactoryImpl.java @@ -4,7 +4,7 @@ * Copyright (c) 2004, Open Cloud Limited. * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v 1.14 2007/10/08 01:49:59 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/core/v3/ConnectionFactoryImpl.java,v 1.15 2008/01/08 06:56:27 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -120,7 +120,7 @@ public ProtocolConnection openConnectionImpl(String host, int port, String user, // Added by Peter Mount // 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); + 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) { @@ -172,7 +172,7 @@ private PGStream enableSSL(PGStream pgStream, boolean requireSSL, Properties inf // 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); + throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_REJECTED); // We have to reconnect to continue. pgStream.close(); @@ -184,7 +184,7 @@ private PGStream enableSSL(PGStream pgStream, boolean requireSSL, Properties inf // Server does not support ssl if (requireSSL) - throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_FAILURE); + throw new PSQLException(GT.tr("The server does not support SSL."), PSQLState.CONNECTION_REJECTED); return pgStream; @@ -197,7 +197,7 @@ private PGStream enableSSL(PGStream pgStream, boolean requireSSL, Properties inf return pgStream; default: - throw new PSQLException(GT.tr("An error occured while setting up the SSL connection."), PSQLState.CONNECTION_FAILURE); + throw new PSQLException(GT.tr("An error occured while setting up the SSL connection."), PSQLState.PROTOCOL_VIOLATION); } } @@ -379,7 +379,7 @@ private void doAuthentication(PGStream pgStream, String user, String password, L break; default: - throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT); + throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION); } } } @@ -422,7 +422,7 @@ private void readStartupMessages(PGStream pgStream, ProtocolConnectionImpl proto // 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); + throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION); int pid = pgStream.ReceiveInteger4(); int ckey = pgStream.ReceiveInteger4(); @@ -468,7 +468,7 @@ private void readStartupMessages(PGStream pgStream, ProtocolConnectionImpl proto 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); + 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")) @@ -478,7 +478,7 @@ else if (name.equals("standard_conforming_strings")) else if (value.equals("off")) protoConnection.setStandardConformingStrings(false); else - throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.CONNECTION_UNABLE_TO_CONNECT); + throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION); } break; @@ -486,7 +486,7 @@ else if (value.equals("off")) 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); + throw new PSQLException(GT.tr("Protocol error. Session setup failed."), PSQLState.PROTOCOL_VIOLATION); } } } diff --git a/org/postgresql/util/PSQLState.java b/org/postgresql/util/PSQLState.java index f64e1b45f9..373738a1f2 100644 --- a/org/postgresql/util/PSQLState.java +++ b/org/postgresql/util/PSQLState.java @@ -3,7 +3,7 @@ * Copyright (c) 2003-2008, PostgreSQL Global Development Group * * IDENTIFICATION -* $PostgreSQL: pgjdbc/org/postgresql/util/PSQLState.java,v 1.12 2007/12/01 12:50:45 jurka Exp $ +* $PostgreSQL: pgjdbc/org/postgresql/util/PSQLState.java,v 1.13 2008/01/08 06:56:31 jurka Exp $ * *------------------------------------------------------------------------- */ @@ -37,12 +37,34 @@ public PSQLState(String state) 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");