diff --git a/org/postgresql/Driver.java.in b/org/postgresql/Driver.java.in index 76e0800..f6c69c0 100644 --- a/org/postgresql/Driver.java.in +++ b/org/postgresql/Driver.java.in @@ -10,7 +10,6 @@ package org.postgresql; 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.PSQLException; 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 @@ public class Driver implements java.sql.Driver * @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 @@ public class Driver implements java.sql.Driver /** * @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; } /** diff --git a/org/postgresql/core/ConnectionFactory.java b/org/postgresql/core/ConnectionFactory.java index 569d599..a351619 100644 --- a/org/postgresql/core/ConnectionFactory.java +++ b/org/postgresql/core/ConnectionFactory.java @@ -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 @@ public abstract class ConnectionFactory { *
* 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 @@ public abstract class ConnectionFactory {
* @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 @@ public abstract class ConnectionFactory {
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 @@ public abstract class ConnectionFactory {
* 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 @@ public abstract class ConnectionFactory {
* @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;
}
diff --git a/org/postgresql/core/PGStream.java b/org/postgresql/core/PGStream.java
index 3c75ee5..fad4c12 100644
--- a/org/postgresql/core/PGStream.java
+++ b/org/postgresql/core/PGStream.java
@@ -19,6 +19,7 @@ import java.net.Socket;
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 @@ import org.postgresql.util.PSQLException;
*/
public class PGStream
{
- private final InetSocketAddress address;
+ private final HostSpec hostSpec;
private final byte[] _int4buf;
private final byte[] _int2buf;
@@ -48,16 +49,15 @@ public class PGStream
* 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 @@ public class PGStream
_int4buf = new byte[4];
}
- public InetSocketAddress getAddress() {
- return address;
+ public HostSpec getHostSpec() {
+ return hostSpec;
}
public Socket getSocket() {
diff --git a/org/postgresql/core/ProtocolConnection.java b/org/postgresql/core/ProtocolConnection.java
index 153c277..62c0f1b 100644
--- a/org/postgresql/core/ProtocolConnection.java
+++ b/org/postgresql/core/ProtocolConnection.java
@@ -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 @@ public interface ProtocolConnection {
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.
diff --git a/org/postgresql/core/v2/ConnectionFactoryImpl.java b/org/postgresql/core/v2/ConnectionFactoryImpl.java
index 90523a1..a0cfc96 100644
--- a/org/postgresql/core/v2/ConnectionFactoryImpl.java
+++ b/org/postgresql/core/v2/ConnectionFactoryImpl.java
@@ -15,7 +15,6 @@ import java.sql.SQLException;
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.PSQLState;
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 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
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 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
// - 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 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
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 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
// 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 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
}
}
- if (addr+1 < addresses.length) {
+ if (whichHost + 1 < hostSpecs.length) {
// still more addresses to try
continue;
}
@@ -165,7 +165,7 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
}
}
- if (addr+1 < addresses.length) {
+ if (whichHost + 1 < hostSpecs.length) {
// still more addresses to try
continue;
}
@@ -199,7 +199,7 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
// We have to reconnect to continue.
pgStream.close();
- return new PGStream(pgStream.getAddress());
+ return new PGStream(pgStream.getHostSpec());
case 'N':
if (logger.logDebug())
diff --git a/org/postgresql/core/v2/ProtocolConnectionImpl.java b/org/postgresql/core/v2/ProtocolConnectionImpl.java
index 567ad3e..204ad9f 100644
--- a/org/postgresql/core/v2/ProtocolConnectionImpl.java
+++ b/org/postgresql/core/v2/ProtocolConnectionImpl.java
@@ -11,12 +11,12 @@ package org.postgresql.core.v2;
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 @@ class ProtocolConnectionImpl implements ProtocolConnection {
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 @@ class ProtocolConnectionImpl implements ProtocolConnection {
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);
diff --git a/org/postgresql/core/v3/ConnectionFactoryImpl.java b/org/postgresql/core/v3/ConnectionFactoryImpl.java
index d50c07d..95eaa43 100644
--- a/org/postgresql/core/v3/ConnectionFactoryImpl.java
+++ b/org/postgresql/core/v3/ConnectionFactoryImpl.java
@@ -14,7 +14,6 @@ import java.util.TimeZone;
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.ServerErrorMessage;
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 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
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 @@ public class ConnectionFactoryImpl extends ConnectionFactory {
//
// Change by Chris Smith