Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-382] avoid connection leak if max_connections is reached
  • Loading branch information
rusher committed Nov 1, 2016
1 parent 4a52270 commit a8f8ab9
Showing 1 changed file with 53 additions and 36 deletions.
Expand Up @@ -363,53 +363,70 @@ public void connect() throws QueryException {
* @throws IOException : connection error (host/port not available)
*/
private void connect(String host, int port) throws QueryException, IOException {
try {
socket = Utils.createSocket(urlParser, host);
initializeSocketOption();

// Bind the socket to a particular interface if the connection property
// localSocketAddress has been defined.
if (options.localSocketAddress != null) {
InetSocketAddress localAddress = new InetSocketAddress(options.localSocketAddress, 0);
socket.bind(localAddress);
}

socket = Utils.createSocket(urlParser, host);
initializeSocketOption();

// Bind the socket to a particular interface if the connection property
// localSocketAddress has been defined.
if (options.localSocketAddress != null) {
InetSocketAddress localAddress = new InetSocketAddress(options.localSocketAddress, 0);
socket.bind(localAddress);
}
if (!socket.isConnected()) {
InetSocketAddress sockAddr = urlParser.getOptions().pipe == null ? new InetSocketAddress(host, port) : null;
if (options.connectTimeout != null) {
socket.connect(sockAddr, options.connectTimeout);
} else {
socket.connect(sockAddr);
}
}

if (!socket.isConnected()) {
InetSocketAddress sockAddr = urlParser.getOptions().pipe == null ? new InetSocketAddress(host, port) : null;
if (options.connectTimeout != null) {
socket.connect(sockAddr, options.connectTimeout);
} else {
socket.connect(sockAddr);
// Extract socketTimeout URL parameter
if (options.socketTimeout != null) {
socket.setSoTimeout(options.socketTimeout);
}
}

// Extract socketTimeout URL parameter
if (options.socketTimeout != null) {
socket.setSoTimeout(options.socketTimeout);
}
handleConnectionPhases();

handleConnectionPhases();
if (options.useCompression) {
writer.setUseCompression(true);
packetFetcher = new ReadPacketFetcher(new DecompressInputStream(socket.getInputStream()), options.maxQuerySizeToLog);
}
connected = true;

if (options.useCompression) {
writer.setUseCompression(true);
packetFetcher = new ReadPacketFetcher(new DecompressInputStream(socket.getInputStream()), options.maxQuerySizeToLog);
}
connected = true;
writer.forceCleanupBuffer();

writer.forceCleanupBuffer();
loadServerData();
setSessionOptions();
writer.setMaxAllowedPacket(Integer.parseInt(serverData.get("max_allowed_packet")));

loadServerData();
setSessionOptions();
writer.setMaxAllowedPacket(Integer.parseInt(serverData.get("max_allowed_packet")));
createDatabaseIfNotExist();
loadCalendar();

createDatabaseIfNotExist();
loadCalendar();

activeStreamingResult = null;
moreResults = false;
hasWarnings = false;
hostFailed = false;
} catch (IOException ioException) {
ensureClosingSocketOnException();
throw ioException;
} catch (QueryException queryException) {
ensureClosingSocketOnException();
throw queryException;
}
}

activeStreamingResult = null;
moreResults = false;
hasWarnings = false;
hostFailed = false;
private void ensureClosingSocketOnException() {
if (socket != null) {
try {
socket.close();
} catch (IOException ioe) {
//eat exception
}
}
}

/**
Expand Down

0 comments on commit a8f8ab9

Please sign in to comment.