title: SpringBoot 2 - Monitor DB connection
body: <p>I have configured a DB Connection (HikariCP) on SprintBoot 2. 
  I have the poolName defined in the <strong>application.properties</strong>:</p>
  
  <pre><code>spring.datasource.hikari.poolName=HikariConnectionPool
  </code></pre>
  
  <p>I want to monitor the number of idle DB connections, so I tried to create a thread which monitors the DB connections as:</p>
  
  <pre><code>public class HikariCPStatistics implements Runnable {
  
      public static final Logger logger = LoggerFactory.getLogger(HikariCPStatistics.class);
  
      private static HikariPoolMXBean poolProxy;
  
      public HikariCPStatistics() {
              MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
              try {
                      ObjectName poolName = new ObjectName("com.zaxxer.hikari:type=Pool (HikariConnectionPool)");
                      poolProxy = JMX.newMXBeanProxy(mBeanServer, poolName, HikariPoolMXBean.class);
  
              } catch (MalformedObjectNameException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
              }
      }
  
      @Override
      public void run() {
              logger.info("Starting Connection Pool status monitor");
              while ( true ) {
                      logger.info("Hikari - Idle Connections: " + poolProxy.getIdleConnections());
                      try {
                              Thread.sleep(5000);
                      } catch (InterruptedException e) {
                              logger.error("Fail to run Connection Pool status monitor");
                              e.printStackTrace();
                      }
              }
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p>but I'm getting an exception that it's not able to find the instance of the poolName <strong>HikariConnectionPool</strong>.
  Any suggestion on how I can get the idle DB connections managed by HikariCP?</p>
  
  <hr>
  
  <p>further analyzes, I could see that SpringBoot is not actually registering the <strong>Pool</strong> bean in JMX.
  SpringBoot is only publishing the <strong>HikariDataSource</strong> bean in JMX, which have the static configuration of Hikari dataSource.
  From what I can see, Hikari supports 2 differents configuration patterns:<br>
  1) configuration though the HikariConfig class <br>
  2) direct configuration of the HikariDataSource <br>
  SpringBoot is using the 2nd option.<br>
  I have tried in the past the 1st option configuring HikariCP (no SpringBoot) which was registering <strong>Pool</strong> and <strong>PoolConfig</strong> Beans.... The <strong>Pool</strong> bean is the one which I'm interested which has the usage of the connections.</p>
  
  <p>Does anybody know how I can configure as option (1 - HikariConfig class) instated HikariDataSource in SpringBoot? Or somehow springboot to create a <strong>Pool</strong> Bean?</p>
  
  <p>Thanks.</p>