title: HikariCP try-with-resources Connection leaking
body: <p>I am working on something where I need to pull data from MariaDB (using HikariCP), and then send it through Redis. Eventually, when I try to pull from the database, the connection will start leaking. This only happens over time, and suddenly.</p>
  
  <p>Here is the full log from when the leak started happening: <a href="https://hastebin.com/sekiximehe.makefile" rel="nofollow noreferrer">https://hastebin.com/sekiximehe.makefile</a></p>
  
  <p>Here is some debug info:</p>
  
  <pre><code>21:04:40 [INFO] 21:04:40.680 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Before cleanup stats (total=6, active=2, idle=4, waiting=0)
  
  21:04:40 [INFO] 21:04:40.680 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - After cleanup  stats (total=6, active=2, idle=4, waiting=0)
  
  21:04:40 [INFO] 21:04:40.682 [HikariPool-1 connection adder] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection org.mariadb.jdbc.MariaDbConnection@4b7a5e97
  
  21:04:40 [INFO] 21:04:40.682 [HikariPool-1 connection adder] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - After adding stats (total=7, active=2, idle=5, waiting=0)
  
  21:05:05 [INFO] 21:05:05.323 [HikariPool-1 housekeeper] WARN  com.zaxxer.hikari.pool.ProxyLeakTask - Connection leak detection triggered for org.mariadb.jdbc.MariaDbConnection@52ede989 on thread Thread-272, stack trace follows
  java.lang.Exception: Apparent connection leak detected
          at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:123)
          at us.survivewith.bungee.database.FetchPlayerInfo.run(FetchPlayerInfo.java:29)
          at java.lang.Thread.run(Thread.java:748)
  
  21:05:10 [INFO] 21:05:10.681 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Before cleanup stats (total=7, active=2, idle=5, waiting=0)
  
  21:05:10 [INFO] 21:05:10.681 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - After cleanup  stats (total=7, active=2, idle=5, waiting=0)
  
  21:05:39 [INFO] 21:05:39.352 [HikariPool-1 housekeeper] WARN  com.zaxxer.hikari.pool.ProxyLeakTask - Connection leak detection triggered for org.mariadb.jdbc.MariaDbConnection@3cba7850 on thread Thread-274, stack trace follows
  java.lang.Exception: Apparent connection leak detected
          at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:123)
          at us.survivewith.bungee.database.FetchPlayerInfo.run(FetchPlayerInfo.java:29)
          at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>Here is the FetchPlayerInfo.run() method:</p>
  
  <pre><code>@Override
  public void run()
  {
      String select = "SELECT `Rank`,`Playtime` FROM `Players` WHERE PlayerUUID=?;";
  
      // This is line 29. How can this possibly be causing a leak?
      try(Connection connection = Database.getHikari().getConnection())
      {
          // Get the data by querying the Players table
          try(PreparedStatement serverSQL = connection.prepareStatement(select))
          {
              serverSQL.setString(1, player);
  
              // Execute statement
              try(ResultSet serverRS = serverSQL.executeQuery())
              {
                  // If a row exists
                  if(serverRS.next())
                  {
                      String rank = serverRS.getString("Rank");
  
                      Jedis jPublisher = Redis.getJedis().getResource();
                      jPublisher.publish("playerconnections", player + "~" + serverRS.getInt("Playtime") + "~" + rank);
                  }
                  else
                  {
                      Jedis jPublisher = Redis.getJedis().getResource();
                      jPublisher.publish("playerconnections", player + "~" + 0 + "~DEFAULT");
                  }
              }
          }
      }
      catch(SQLException e)
      {
          //Print out any exception while trying to prepare statement
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>This is how I've setup my Database class:</p>
  
  <pre><code>/**
   * This class is used to connect to the database
   */
  public class Database
  {
      private static HikariDataSource hikari;
  
      /**
       * Connects to the database
       */
      public static void connectToDatabase(String address,
                                           String db,
                                           String user,
                                           String password,
                                           int port)
      {
          // Setup main Hikari instance
          hikari = new HikariDataSource();
          hikari.setMaximumPoolSize(20);
          hikari.setLeakDetectionThreshold(60 * 1000);
          hikari.setDataSourceClassName("org.mariadb.jdbc.MariaDbDataSource");
          hikari.addDataSourceProperty("serverName", address);
          hikari.addDataSourceProperty("port", port);
          hikari.addDataSourceProperty("databaseName", db);
          hikari.addDataSourceProperty("user", user);
          hikari.addDataSourceProperty("password", password);
      }
  
      /**
       * Returns an instance of Hikari.
       * This instance is connected to the database that contains all data.
       * The stats table is only used in this database every other day
       *
       * @return The main HikariDataSource
       */
      public static HikariDataSource getHikari()
      {
          return hikari;
      }
  </code></pre>
  
  <p>And this is how I am calling the FetchPlayerInfo class:</p>
  
  <pre><code>new Thread(new FetchPlayerInfo(player.getUniqueId().toString())).start();
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>The problem still persists after using a synchronized getConnection() method from the Database class. </p>