// backward-compatible code here.
//
// Change by Chris Smith <cdsmith@twu.net>
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 " + hostSpec);
//
// Establish a connection.
//
int connectTimeout = 0;
String connectTimeoutProperty = info.getProperty("connectTimeout", "0");
try {
connectTimeout = Integer.parseInt(connectTimeoutProperty) * 1000;
} catch (NumberFormatException nfe) {
logger.info("Couldn't parse connectTimeout value:" + connectTimeoutProperty);
}
PGStream newStream = null;
try
{
newStream = new PGStream(hostSpec, connectTimeout);
// Construct and send an ssl startup packet if requested.
if (trySSL)
newStream = enableSSL(newStream, requireSSL, info, logger, connectTimeout);
// Set the socket timeout if the "socketTimeout" property has been set.
String socketTimeoutProperty = info.getProperty("socketTimeout", "0");
try {
int socketTimeout = Integer.parseInt(socketTimeoutProperty);
if (socketTimeout > 0) {
newStream.getSocket().setSoTimeout(socketTimeout*1000);
}
} catch (NumberFormatException nfe) {
logger.info("Couldn't parse socketTimeout value:" + socketTimeoutProperty);
}
// Enable TCP keep-alive probe if required.
newStream.getSocket().setKeepAlive(requireTCPKeepAlive);
// Try to set SO_SNDBUF and SO_RECVBUF socket options, if requested.
// If receiveBufferSize and send_buffer_size are set to a value greater
// than 0, adjust. -1 means use the system default, 0 is ignored since not
// supported.
// Set SO_RECVBUF read buffer size
String receiveBufferSizeProperty = info.getProperty("receiveBufferSize", "-1");
try {
int receiveBufferSize = Integer.parseInt(receiveBufferSizeProperty);
if (receiveBufferSize > -1) {
// value of 0 not a valid buffer size value
if (receiveBufferSize > 0) {
newStream.getSocket().setReceiveBufferSize(receiveBufferSize);
} else {
logger.info("Ignore invalid value for receiveBufferSize: " + receiveBufferSize);
}
}
} catch (NumberFormatException nfe) {
logger.info("Couldn't parse receiveBufferSize value: " + receiveBufferSizeProperty);
}
// Set SO_SNDBUF write buffer size
String sendBufferSizeProperty = info.getProperty("sendBufferSize", "-1");
try {
int sendBufferSize = Integer.parseInt(sendBufferSizeProperty);
if (sendBufferSize > -1) {
if (sendBufferSize > 0) {
newStream.getSocket().setSendBufferSize(sendBufferSize);
} else {
logger.info("Ignore invalid value for sendBufferSize: " + sendBufferSize);
}
}
} catch (NumberFormatException nfe) {
logger.info("Couldn't parse sendBufferSize value: " + sendBufferSizeProperty);
}
logger.info("Receive Buffer Size is " + newStream.getSocket().getReceiveBufferSize());
logger.info("Send Buffer Size is " + newStream.getSocket().getSendBufferSize());
List<String[]> paramList = new ArrayList<String[]>();
paramList.add(new String[] {"user", user});
paramList.add(new String[] {"database", database});
paramList.add(new String[] {"client_encoding", "UTF8"});
paramList.add(new String[] {"DateStyle", "ISO"});
paramList.add(new String[] {"TimeZone", createPostgresTimeZone()});
String assumeMinServerVersion = info.getProperty("assumeMinServerVersion");
if( Utils.parseServerVersionStr(assumeMinServerVersion) >= 90000 ) {
// User is explicitly telling us this is a 9.0+ server so set properties here:
paramList.add(new String[] {"extra_float_digits", "3"});
String appName = info.getProperty("ApplicationName");
if( appName != null ) {
paramList.add(new String[] {"application_name", appName});
}
} else {
// User has not explicitly told us that this is a 9.0+ server so stick to old default:
paramList.add(new String[] {"extra_float_digits", "2"});
}
String[][] params = paramList.toArray(new String[][]{});
sendStartupPacket(newStream, params, logger);
// Do authentication (until AuthenticationOk).
doAuthentication(newStream, hostSpec.getHost(), user, info, logger);
// Do final startup.
ProtocolConnectionImpl protoConnection = new ProtocolConnectionImpl(newStream, user, database, info, logger, connectTimeout);
readStartupMessages(newStream, protoConnection, logger);