brettwooldridge / HikariCP

光 HikariCP・A solid, high-performance, JDBC connection pool at last.
Apache License 2.0
19.65k stars 2.9k forks source link

PoolBase.quietlyCloseConnection() calls close twice #2152

Closed bjorndarri closed 5 months ago

bjorndarri commented 7 months ago

I'm using a Proxy wrapper around a Hikari pool, counting connections created and destroyed.

I noticed that my destroy count seemed to be off, and on further inspection it turned out that it was doubled.

Looks like the problem is in the try with resources clause, where connection is specified twice, resulting in it being closed twice:

void quietlyCloseConnection(final Connection connection, final String closureReason)
{
   if (connection != null) {
      try {
         logger.debug("{} - Closing connection {}: {}", poolName, connection, closureReason);

         // continue with the close even if setNetworkTimeout() throws
         try (connection; connection) { // <<<<<<<<
            if (!connection.isClosed())
               setNetworkTimeout(connection, SECONDS.toMillis(15));
         } catch (SQLException e) {
            // ignore
         }
      }
      catch (Exception e) {
         logger.debug("{} - Closing connection {} failed", poolName, connection, e);
      }
   }
}

This doesn't really cause any problems, since ProxyConnection.close() doesn't do anything if the connection is already closed, but I figured I'd point this out anyway. I've added a similar guard around my count method.

I'm ready to create a pull request if required.

quaff commented 7 months ago

Good catch.