Index: org/postgresql/jdbc3g/Jdbc3gConnection.java =================================================================== --- org/postgresql/jdbc3g/Jdbc3gConnection.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/jdbc3g/Jdbc3gConnection.java (revision ) @@ -7,11 +7,12 @@ */ package org.postgresql.jdbc3g; -import java.net.InetSocketAddress; import java.util.Map; import java.util.Properties; import java.sql.SQLException; +import org.postgresql.util.HostSpec; + /** * This class implements the java.sql.Connection interface for JDBC3. * However most of the implementation is really done in @@ -19,8 +20,8 @@ */ public class Jdbc3gConnection extends org.postgresql.jdbc3g.AbstractJdbc3gConnection implements java.sql.Connection { - public Jdbc3gConnection(InetSocketAddress[] address, String user, String database, Properties info, String url) throws SQLException { - super(address, user, database, info, url); + public Jdbc3gConnection(HostSpec[] hostSpecs, String user, String database, Properties info, String url) throws SQLException { + super(hostSpecs, user, database, info, url); } public java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Index: org/postgresql/jdbc4/Jdbc4Connection.java =================================================================== --- org/postgresql/jdbc4/Jdbc4Connection.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/jdbc4/Jdbc4Connection.java (revision ) @@ -7,11 +7,12 @@ */ package org.postgresql.jdbc4; -import java.net.InetSocketAddress; import java.util.Map; import java.util.Properties; import java.sql.SQLException; +import org.postgresql.util.HostSpec; + /** * This class implements the java.sql.Connection interface for JDBC4. * However most of the implementation is really done in @@ -19,8 +20,8 @@ */ public class Jdbc4Connection extends AbstractJdbc4Connection implements java.sql.Connection { - public Jdbc4Connection(InetSocketAddress[] address, String user, String database, Properties info, String url) throws SQLException { - super(address, user, database, info, url); + public Jdbc4Connection(HostSpec[] hostSpecs, String user, String database, Properties info, String url) throws SQLException { + super(hostSpecs, user, database, info, url); } public java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Index: org/postgresql/jdbc3/AbstractJdbc3Connection.java =================================================================== --- org/postgresql/jdbc3/AbstractJdbc3Connection.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/jdbc3/AbstractJdbc3Connection.java (revision ) @@ -7,10 +7,10 @@ */ package org.postgresql.jdbc3; -import java.net.InetSocketAddress; import java.util.Properties; import java.sql.*; +import org.postgresql.util.HostSpec; import org.postgresql.util.PSQLException; import org.postgresql.util.PSQLState; import org.postgresql.util.GT; @@ -25,8 +25,8 @@ private int rsHoldability = ResultSet.CLOSE_CURSORS_AT_COMMIT; private int savepointId = 0; - protected AbstractJdbc3Connection(InetSocketAddress[] address, String user, String database, Properties info, String url) throws SQLException { - super(address, user, database, info, url); + protected AbstractJdbc3Connection(HostSpec[] hostSpec, String user, String database, Properties info, String url) throws SQLException { + super(hostSpec, user, database, info, url); } /** Index: org/postgresql/core/v2/ProtocolConnectionImpl.java =================================================================== --- org/postgresql/core/v2/ProtocolConnectionImpl.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/core/v2/ProtocolConnectionImpl.java (revision ) @@ -11,12 +11,12 @@ import java.sql.SQLException; import java.sql.SQLWarning; import java.io.IOException; -import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Set; import org.postgresql.PGNotification; import org.postgresql.core.*; +import org.postgresql.util.HostSpec; /** * V2 implementation of ProtocolConnection. @@ -32,8 +32,8 @@ this.executor = new QueryExecutorImpl(this, pgStream, logger); } - public InetSocketAddress getAddress() { - return pgStream.getAddress(); + public HostSpec getHostSpec() { + return pgStream.getHostSpec(); } public String getUser() { @@ -87,7 +87,7 @@ if (logger.logDebug()) logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")"); - cancelStream = new PGStream(pgStream.getAddress()); + cancelStream = new PGStream(pgStream.getHostSpec()); cancelStream.SendInteger4(16); cancelStream.SendInteger2(1234); cancelStream.SendInteger2(5678); Index: org/postgresql/jdbc3/Jdbc3Connection.java =================================================================== --- org/postgresql/jdbc3/Jdbc3Connection.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/jdbc3/Jdbc3Connection.java (revision ) @@ -7,11 +7,12 @@ */ package org.postgresql.jdbc3; -import java.net.InetSocketAddress; import java.util.Map; import java.util.Properties; import java.sql.SQLException; +import org.postgresql.util.HostSpec; + /** * This class implements the java.sql.Connection interface for JDBC3. * However most of the implementation is really done in @@ -19,8 +20,8 @@ */ public class Jdbc3Connection extends org.postgresql.jdbc3.AbstractJdbc3Connection implements java.sql.Connection { - public Jdbc3Connection(InetSocketAddress[] address,, String user, String database, Properties info, String url) throws SQLException { - super(address, user, database, info, url); + public Jdbc3Connection(HostSpec[] hostSpecs,, String user, String database, Properties info, String url) throws SQLException { + super(hostSpecs, user, database, info, url); } public java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException Index: org/postgresql/core/v3/ConnectionFactoryImpl.java =================================================================== --- org/postgresql/core/v3/ConnectionFactoryImpl.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/core/v3/ConnectionFactoryImpl.java (revision ) @@ -14,7 +14,6 @@ import java.sql.SQLException; import java.io.IOException; import java.net.ConnectException; -import java.net.InetSocketAddress; import org.postgresql.core.*; import org.postgresql.util.PSQLException; @@ -24,6 +23,7 @@ import org.postgresql.util.UnixCrypt; import org.postgresql.util.MD5Digest; import org.postgresql.util.GT; +import org.postgresql.util.HostSpec; /** * ConnectionFactory implementation for version 3 (7.4+) connections. @@ -46,7 +46,7 @@ private static class UnsupportedProtocolException extends IOException { } - public ProtocolConnection openConnectionImpl(InetSocketAddress[] addresses, String user, String database, Properties info, Logger logger) throws SQLException { + public ProtocolConnection openConnectionImpl(HostSpec[] hostSpecs, String user, String database, Properties info, Logger logger) throws SQLException { // Extract interesting values from the info properties: // - the SSL setting boolean requireSSL; @@ -85,11 +85,11 @@ // // Change by Chris Smith - for (int addr = 0; addr < addresses.length; ++addr) { - InetSocketAddress address = addresses[addr]; + for (int whichHost = 0; whichHost < hostSpecs.length; ++whichHost) { + HostSpec hostSpec = hostSpecs[whichHost]; if (logger.logDebug()) - logger.debug("Trying to establish a protocol version 3 connection to " + address); + logger.debug("Trying to establish a protocol version 3 connection to " + hostSpec); // // Establish a connection. @@ -98,7 +98,7 @@ PGStream newStream = null; try { - newStream = new PGStream(address); + newStream = new PGStream(hostSpec); // Construct and send an ssl startup packet if requested. if (trySSL) @@ -170,7 +170,7 @@ sendStartupPacket(newStream, params, logger); // Do authentication (until AuthenticationOk). - doAuthentication(newStream, address.getHostName(), user, info, logger); + doAuthentication(newStream, hostSpec.getHost(), user, info, logger); // Do final startup. ProtocolConnectionImpl protoConnection = new ProtocolConnectionImpl(newStream, user, database, info, logger); @@ -200,7 +200,7 @@ // 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 - if (addr+1 < addresses.length) { + if (whichHost + 1 < hostSpecs.length) { // still more addresses to try continue; } @@ -218,7 +218,7 @@ { } } - if (addr+1 < addresses.length) { + if (whichHost + 1 < hostSpecs.length) { // still more addresses to try continue; } @@ -236,7 +236,7 @@ { } } - if (addr+1 < addresses.length) { + if (whichHost + 1 < hostSpecs.length) { // still more addresses to try continue; } @@ -296,7 +296,7 @@ // We have to reconnect to continue. pgStream.close(); - return new PGStream(pgStream.getAddress()); + return new PGStream(pgStream.getHostSpec()); case 'N': if (logger.logDebug()) Index: org/postgresql/core/ConnectionFactory.java =================================================================== --- org/postgresql/core/ConnectionFactory.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/core/ConnectionFactory.java (revision ) @@ -8,10 +8,10 @@ */ package org.postgresql.core; -import java.net.InetSocketAddress; import java.util.Properties; import java.sql.SQLException; +import org.postgresql.util.HostSpec; import org.postgresql.util.PSQLException; import org.postgresql.util.GT; import org.postgresql.util.PSQLState; @@ -42,8 +42,7 @@ *

* Currently, protocol versions 3 (7.4+) and 2 (pre-7.4) are supported. * - * @param host the host to connect to - * @param port the port to connect to + * @param hostSpecs at least one host and port to connect to; multiple elements for round-robin failover * @param user the username to authenticate with; may not be null. * @param database the database on the server to connect to; may not be null. * @param info extra properties controlling the connection; @@ -52,7 +51,7 @@ * @return the new, initialized, connection * @throws SQLException if the connection could not be established. */ - public static ProtocolConnection openConnection(InetSocketAddress[] address, String user, String database, Properties info, Logger logger) throws SQLException { + public static ProtocolConnection openConnection(HostSpec[] hostSpecs, String user, String database, Properties info, Logger logger) throws SQLException { String protoName = info.getProperty("protocolVersion"); for (int i = 0; i < versions.length; ++i) @@ -62,7 +61,7 @@ continue; ConnectionFactory factory = (ConnectionFactory) versions[i][1]; - ProtocolConnection connection = factory.openConnectionImpl(address, user, database, info, logger); + ProtocolConnection connection = factory.openConnectionImpl(hostSpecs, user, database, info, logger); if (connection != null) return connection; } @@ -75,8 +74,7 @@ * Implementation of {@link #openConnection} for a particular protocol version. * Implemented by subclasses of {@link ConnectionFactory}. * - * @param host the host to connect to - * @param port the port to connect to + * @param hostSpecs at least one host and port to connect to; multiple elements for round-robin failover * @param user the username to authenticate with; may not be null. * @param database the database on the server to connect to; may not be null. * @param info extra properties controlling the connection; @@ -87,5 +85,5 @@ * @throws SQLException if the connection could not be established for a reason other * than protocol version incompatibility. */ - public abstract ProtocolConnection openConnectionImpl(InetSocketAddress[] address, String user, String database, Properties info, Logger logger) throws SQLException; + public abstract ProtocolConnection openConnectionImpl(HostSpec[] hostSpecs, String user, String database, Properties info, Logger logger) throws SQLException; } Index: org/postgresql/core/PGStream.java =================================================================== --- org/postgresql/core/PGStream.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/core/PGStream.java (revision ) @@ -19,6 +19,7 @@ import java.sql.SQLException; import org.postgresql.util.GT; +import org.postgresql.util.HostSpec; import org.postgresql.util.PSQLState; import org.postgresql.util.PSQLException; @@ -31,7 +32,7 @@ */ public class PGStream { - private final InetSocketAddress address; + private final HostSpec hostSpec; private final byte[] _int4buf; private final byte[] _int2buf; @@ -48,16 +49,15 @@ * Constructor: Connect to the PostgreSQL back end and return * a stream connection. * - * @param host the hostname to connect to - * @param port the port number that the postmaster is sitting on + * @param hostSpec the host and port to connect to * @exception IOException if an IOException occurs below it. */ - public PGStream(InetSocketAddress address) throws IOException + public PGStream(HostSpec hostSpec) throws IOException { - this.address = address; + this.hostSpec = hostSpec; Socket socket = new Socket(); - socket.connect(address); + socket.connect(new InetSocketAddress(hostSpec.getHost(), hostSpec.getPort())); changeSocket(socket); setEncoding(Encoding.getJVMEncoding("US-ASCII")); @@ -65,8 +65,8 @@ _int4buf = new byte[4]; } - public InetSocketAddress getAddress() { - return address; + public HostSpec getHostSpec() { + return hostSpec; } public Socket getSocket() { Index: org/postgresql/jdbc3g/AbstractJdbc3gConnection.java =================================================================== --- org/postgresql/jdbc3g/AbstractJdbc3gConnection.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/jdbc3g/AbstractJdbc3gConnection.java (revision ) @@ -7,18 +7,18 @@ */ package org.postgresql.jdbc3g; -import java.net.InetSocketAddress; import java.sql.SQLException; import java.util.Properties; import org.postgresql.core.Oid; import org.postgresql.core.TypeInfo; +import org.postgresql.util.HostSpec; public abstract class AbstractJdbc3gConnection extends org.postgresql.jdbc3.AbstractJdbc3Connection { - public AbstractJdbc3gConnection(InetSocketAddress[] address, String user, String database, Properties info, String url) throws SQLException { - super(address, user, database, info, url); + public AbstractJdbc3gConnection(HostSpec[] hostSpecs, String user, String database, Properties info, String url) throws SQLException { + super(hostSpecs, user, database, info, url); TypeInfo types = getTypeInfo(); if (haveMinimumServerVersion("8.3")) { Index: org/postgresql/core/v3/ProtocolConnectionImpl.java =================================================================== --- org/postgresql/core/v3/ProtocolConnectionImpl.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/core/v3/ProtocolConnectionImpl.java (revision ) @@ -10,11 +10,11 @@ import org.postgresql.PGNotification; import org.postgresql.core.*; +import org.postgresql.util.HostSpec; import java.sql.SQLException; import java.sql.SQLWarning; import java.io.IOException; -import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Set; import java.util.HashSet; @@ -37,8 +37,8 @@ this.standardConformingStrings = false; } - public InetSocketAddress getAddress() { - return pgStream.getAddress(); + public HostSpec getHostSpec() { + return pgStream.getHostSpec(); } public String getUser() { @@ -89,7 +89,7 @@ if (logger.logDebug()) logger.debug(" FE=> CancelRequest(pid=" + cancelPid + ",ckey=" + cancelKey + ")"); - cancelStream = new PGStream(pgStream.getAddress()); + cancelStream = new PGStream(pgStream.getHostSpec()); cancelStream.SendInteger4(16); cancelStream.SendInteger2(1234); cancelStream.SendInteger2(5678); Index: org/postgresql/jdbc4/AbstractJdbc4Connection.java =================================================================== --- org/postgresql/jdbc4/AbstractJdbc4Connection.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/jdbc4/AbstractJdbc4Connection.java (revision ) @@ -7,7 +7,6 @@ */ package org.postgresql.jdbc4; -import java.net.InetSocketAddress; import java.sql.*; import java.util.Map; import java.util.HashMap; @@ -19,6 +18,7 @@ import org.postgresql.core.Utils; import org.postgresql.core.TypeInfo; import org.postgresql.util.GT; +import org.postgresql.util.HostSpec; import org.postgresql.util.PSQLState; import org.postgresql.util.PSQLException; import org.postgresql.jdbc2.AbstractJdbc2Array; @@ -27,8 +27,8 @@ { private final Properties _clientInfo; - public AbstractJdbc4Connection(InetSocketAddress[] address, String user, String database, Properties info, String url) throws SQLException { - super(address, user, database, info, url); + public AbstractJdbc4Connection(HostSpec[] hostSpecs, String user, String database, Properties info, String url) throws SQLException { + super(hostSpecs, user, database, info, url); TypeInfo types = getTypeInfo(); if (haveMinimumServerVersion("8.3")) { Index: org/postgresql/core/ProtocolConnection.java =================================================================== --- org/postgresql/core/ProtocolConnection.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/core/ProtocolConnection.java (revision ) @@ -9,8 +9,8 @@ package org.postgresql.core; import org.postgresql.PGNotification; +import org.postgresql.util.HostSpec; -import java.net.InetSocketAddress; import java.sql.*; import java.util.Set; @@ -40,9 +40,9 @@ static final int TRANSACTION_FAILED = 2; /** - * @return the address this connection is connected to. + * @return the host and port this connection is connected to. */ - InetSocketAddress getAddress(); + HostSpec getHostSpec(); /** * @return the user this connection authenticated as. Index: org/postgresql/core/v2/ConnectionFactoryImpl.java =================================================================== --- org/postgresql/core/v2/ConnectionFactoryImpl.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/core/v2/ConnectionFactoryImpl.java (revision ) @@ -15,7 +15,6 @@ import java.sql.SQLWarning; import java.io.IOException; import java.net.ConnectException; -import java.net.InetSocketAddress; import org.postgresql.core.*; import org.postgresql.util.PSQLException; @@ -23,6 +22,7 @@ import org.postgresql.util.UnixCrypt; import org.postgresql.util.MD5Digest; import org.postgresql.util.GT; +import org.postgresql.util.HostSpec; /** * ConnectionFactory implementation for version 2 (pre-7.4) connections. @@ -38,7 +38,7 @@ private static final int AUTH_REQ_MD5 = 5; private static final int AUTH_REQ_SCM = 6; - public ProtocolConnection openConnectionImpl(InetSocketAddress[] addresses, String user, String database, Properties info, Logger logger) throws SQLException { + public ProtocolConnection openConnectionImpl(HostSpec[] hostSpecs, String user, String database, Properties info, Logger logger) throws SQLException { // Extract interesting values from the info properties: // - the SSL setting boolean requireSSL; @@ -70,10 +70,10 @@ // - the TCP keep alive setting boolean requireTCPKeepAlive = (Boolean.valueOf(info.getProperty("tcpKeepAlive")).booleanValue()); - for (int addr = 0; addr < addresses.length; ++addr) { - InetSocketAddress address = addresses[addr]; + for (int whichHost = 0; whichHost < hostSpecs.length; ++whichHost) { + HostSpec hostSpec = hostSpecs[whichHost]; if (logger.logDebug()) - logger.debug("Trying to establish a protocol version 2 connection to " + address); + logger.debug("Trying to establish a protocol version 2 connection to " + hostSpec); // // Establish a connection. @@ -83,7 +83,7 @@ PGStream newStream = null; try { - newStream = new PGStream(address); + newStream = new PGStream(hostSpec); // Construct and send an ssl startup packet if requested. if (trySSL) @@ -127,7 +127,7 @@ // ConnectException is thrown when the connection cannot be made. // we trap this an return a more meaningful message for the end user - if (addr+1 < addresses.length) { + if (whichHost + 1 < hostSpecs.length) { // still more addresses to try continue; } @@ -146,7 +146,7 @@ } } - if (addr+1 < addresses.length) { + if (whichHost + 1 < hostSpecs.length) { // still more addresses to try continue; } @@ -165,7 +165,7 @@ } } - if (addr+1 < addresses.length) { + if (whichHost + 1 < hostSpecs.length) { // still more addresses to try continue; } @@ -199,7 +199,7 @@ // We have to reconnect to continue. pgStream.close(); - return new PGStream(pgStream.getAddress()); + return new PGStream(pgStream.getHostSpec()); case 'N': if (logger.logDebug()) Index: org/postgresql/Driver.java.in =================================================================== --- org/postgresql/Driver.java.in (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/Driver.java.in (revision ) @@ -10,7 +10,6 @@ import java.io.*; import java.sql.*; import java.util.*; -import java.net.InetSocketAddress; import java.net.URL; import java.security.AccessController; @@ -21,6 +20,7 @@ import org.postgresql.util.PSQLState; import org.postgresql.util.PSQLDriverVersion; import org.postgresql.util.GT; +import org.postgresql.util.HostSpec; import org.postgresql.core.Logger; @@ -390,7 +390,7 @@ * @throws SQLException if the connection could not be made */ private static Connection makeConnection(String url, Properties props) throws SQLException { - return new @JDBCCONNECTCLASS@(address(props), + return new @JDBCCONNECTCLASS@(hostSpecs(props), user(props), database(props), props, url); } @@ -647,15 +647,15 @@ /** * @return the address portion of the URL */ - private static InetSocketAddress[] address(Properties props) + private static HostSpec[] hostSpecs(Properties props) { String[] hosts = props.getProperty("PGHOST").split(","); String[] ports = props.getProperty("PGPORT").split(","); - InetSocketAddress[] addresses = new InetSocketAddress[hosts.length]; - for (int i = 0; i < addresses.length; ++i) { - addresses[i] = new InetSocketAddress(hosts[i], Integer.parseInt(ports[i])); + HostSpec[] hostSpecs = new HostSpec[hosts.length]; + for (int i = 0; i < hostSpecs.length; ++i) { + hostSpecs[i] = new HostSpec(hosts[i], Integer.parseInt(ports[i])); } - return addresses; + return hostSpecs; } /** Index: org/postgresql/util/HostSpec.java =================================================================== --- org/postgresql/util/HostSpec.java (revision ) +++ org/postgresql/util/HostSpec.java (revision ) @@ -0,0 +1,34 @@ +/*------------------------------------------------------------------------- +* +* Copyright (c) 2004-2011, PostgreSQL Global Development Group +* +* +*------------------------------------------------------------------------- +*/ +package org.postgresql.util; + +/** + * Simple container for host and port. + */ +public class HostSpec { + protected final String host; + protected final int port; + + public HostSpec(String host, int port) + { + this.host = host; + this.port = port; + } + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } + + public String toString() { + return host + ":" + port; + } +} Index: org/postgresql/jdbc2/AbstractJdbc2Connection.java =================================================================== --- org/postgresql/jdbc2/AbstractJdbc2Connection.java (revision 5775ea549397ba3484868cd31082f9f271938b66) +++ org/postgresql/jdbc2/AbstractJdbc2Connection.java (revision ) @@ -13,17 +13,13 @@ import java.util.*; import org.postgresql.core.*; -import java.net.InetSocketAddress; import org.postgresql.Driver; import org.postgresql.PGNotification; import org.postgresql.fastpath.Fastpath; import org.postgresql.largeobject.LargeObjectManager; -import org.postgresql.util.PGBinaryObject; -import org.postgresql.util.PSQLState; -import org.postgresql.util.PGobject; -import org.postgresql.util.PSQLException; -import org.postgresql.util.GT; +import org.postgresql.util.*; +import org.postgresql.util.HostSpec; import org.postgresql.copy.*; /** @@ -86,7 +82,7 @@ // // Ctor. // - protected AbstractJdbc2Connection(InetSocketAddress[] address, String user, String database, Properties info, String url) throws SQLException + protected AbstractJdbc2Connection(HostSpec[] hostSpecs, String user, String database, Properties info, String url) throws SQLException { this.creatingURL = url; @@ -137,7 +133,7 @@ logger.info(Driver.getVersion()); // Now make the initial connection and set up local state - this.protoConnection = ConnectionFactory.openConnection(address, user, database, info, logger); + this.protoConnection = ConnectionFactory.openConnection(hostSpecs, user, database, info, logger); this.dbVersionNumber = protoConnection.getServerVersion(); this.compatible = info.getProperty("compatible", Driver.MAJORVERSION + "." + Driver.MINORVERSION);