Skip to content

Commit

Permalink
Use slightly different SQL State error codes for the different
Browse files Browse the repository at this point in the history
types of connection setup failures to indicate which can be
retried and which cannot.

Per discussion with Donald Fraser and Kevin Grittner.
  • Loading branch information
kjurka committed Mar 31, 2011
1 parent 7036ca4 commit c934074
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
14 changes: 7 additions & 7 deletions org/postgresql/core/v2/ConnectionFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 $
*
*-------------------------------------------------------------------------
*/
Expand Down Expand Up @@ -90,7 +90,7 @@ public ProtocolConnection openConnectionImpl(String host, int port, String user,
// Added by Peter Mount <[email protected]>
// 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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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;

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions org/postgresql/core/v3/ConnectionFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 $
*
*-------------------------------------------------------------------------
*/
Expand Down Expand Up @@ -120,7 +120,7 @@ public ProtocolConnection openConnectionImpl(String host, int port, String user,
// Added by Peter Mount <[email protected]>
// 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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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;

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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"))
Expand All @@ -478,15 +478,15 @@ 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;

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);
}
}
}
Expand Down
24 changes: 23 additions & 1 deletion org/postgresql/util/PSQLState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 $
*
*-------------------------------------------------------------------------
*/
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit c934074

Please sign in to comment.