Skip to content

Commit

Permalink
closes #44 potential connection leak during connection creation in so…
Browse files Browse the repository at this point in the history
…me failure scenarios.
  • Loading branch information
brettwooldridge committed Mar 19, 2014
1 parent 079bf9e commit 99b2b76
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/main/java/com/zaxxer/hikari/HikariPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ private void fillPool()
*/
private void addConnection(final long loginTimeout)
{
Connection connection = null;
try
{
// Speculative increment of totalConnections with expectation of success (first time through)
Expand All @@ -329,7 +330,7 @@ private void addConnection(final long loginTimeout)
}

dataSource.setLoginTimeout((int) loginTimeout);
Connection connection = dataSource.getConnection();
connection = dataSource.getConnection();

transactionIsolation = (transactionIsolation < 0 ? connection.getTransactionIsolation() : transactionIsolation);

Expand All @@ -349,6 +350,11 @@ private void addConnection(final long loginTimeout)
{
// We failed, so undo speculative increment of totalConnections
totalConnections.decrementAndGet();

if (connection != null)
{
quietlyCloseConnection(connection);
}

long now = System.currentTimeMillis();
if (now - lastConnectionFailureTime > 1000 || debug)
Expand Down Expand Up @@ -429,6 +435,18 @@ private void closeConnection(IHikariConnectionProxy connectionProxy)
}
}

private void quietlyCloseConnection(Connection connection)
{
try
{
connection.close();
}
catch (SQLException e)
{
return;
}
}

/**
* Execute the user-specified init SQL.
*
Expand Down

0 comments on commit 99b2b76

Please sign in to comment.