{"record":{"id":"cf209b482aee9a7d","repo":"brettwooldridge/HikariCP","slug":"cannot-use-setmetricregistry-and-setmetricstrack","errorCode":null,"errorMessage":"cannot use setMetricRegistry() and setMetricsTrackerFactory() together","messagePattern":"cannot use setMetricRegistry\\(\\) and setMetricsTrackerFactory\\(\\) together","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/zaxxer/hikari/HikariConfig.java","lineNumber":675,"sourceCode":"   /**\n    * Get the MetricRegistry instance to use for registration of metrics used by HikariCP.  Default is {@code null}.\n    *\n    * @return the MetricRegistry instance that will be used\n    */\n   public Object getMetricRegistry()\n   {\n      return metricRegistry;\n   }\n\n   /**\n    * Set a MetricRegistry instance to use for registration of metrics used by HikariCP.\n    *\n    * @param metricRegistry the MetricRegistry instance to use\n    */\n   public void setMetricRegistry(Object metricRegistry)\n   {\n      if (metricsTrackerFactory != null) {\n         throw new IllegalStateException(\"cannot use setMetricRegistry() and setMetricsTrackerFactory() together\");\n      }\n\n      if (metricRegistry != null) {\n         metricRegistry = getObjectOrPerformJndiLookup(metricRegistry);\n\n         if (!safeIsAssignableFrom(metricRegistry, \"com.codahale.metrics.MetricRegistry\")\n             && !(safeIsAssignableFrom(metricRegistry, \"io.dropwizard.metrics5.MetricRegistry\"))\n             && !(safeIsAssignableFrom(metricRegistry, \"io.micrometer.core.instrument.MeterRegistry\"))) {\n            throw new IllegalArgumentException(\"Class must be instance of com.codahale.metrics.MetricRegistry, \" +\n               \"io.dropwizard.metrics5.MetricRegistry, or io.micrometer.core.instrument.MeterRegistry\");\n         }\n      }\n\n      this.metricRegistry = metricRegistry;\n   }\n\n   /**\n    * Get the HealthCheckRegistry that will be used for registration of health checks by HikariCP.  Currently only","sourceCodeStart":657,"sourceCodeEnd":693,"githubUrl":"https://github.com/brettwooldridge/HikariCP/blob/a4d93f4f85517f90e632b795486d7102e933d7ff/src/main/java/com/zaxxer/hikari/HikariConfig.java#L657-L693","documentation":"Mirror of the setMetricsTrackerFactory() guard: setMetricRegistry(Object) throws IllegalStateException if a MetricsTrackerFactory has already been configured. HikariCP only allows one metrics backend, and a previously set factory means the registry would be ignored or double-report. Ordering matters — whichever setter runs second loses.","triggerScenarios":"config.setMetricsTrackerFactory(factory) followed by config.setMetricRegistry(registry); or properties-based configuration setting metricsTrackerFactory and then code (often framework autoconfiguration such as Spring Boot's HikariMetricsInitializer) setting the registry on the same HikariConfig instance afterwards.","commonSituations":"Spring Boot autoconfigures Micrometer metrics for HikariCP when micrometer is present, colliding with a hand-rolled MetricsTrackerFactory in application code; property files left over from a previous metrics approach; multi-module projects where each module touches the shared pool config.","solutions":["Remove one of the two: either drop the custom MetricsTrackerFactory or stop passing the registry","In Spring Boot, exclude or rely on Hikari metric autoconfiguration rather than fighting it — set spring.datasource.hikari.* accordingly","If both are needed in different environments, make the config assembly conditional (e.g. @ConditionalOnMissingBean) instead of unconditional","Order and guard: check config.getMetricsTrackerFactory() == null before setting the registry"],"exampleFix":"// before\nconfig.setMetricsTrackerFactory(customFactory);\nconfig.setMetricRegistry(registry); // IllegalStateException\n\n// after\nif (config.getMetricsTrackerFactory() == null) {\n   config.setMetricRegistry(registry);\n}","handlingStrategy":"validation","validationCode":"if (config.getMetricsTrackerFactory() == null) {\n   config.setMetricRegistry(registry);\n} // else: custom factory already installed","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make metrics wiring conditional (e.g. @ConditionalOnMissingBean) in framework code","Audit property-based config for both metricRegistry and metricsTrackerFactory keys","Keep one component responsible for all HikariCP metrics configuration"],"tags":["hikaricp","configuration","metrics","micrometer","dropwizard"],"backgroundTag":null,"analyzedSha":"a4d93f4f85517f90e632b795486d7102e933d7ff","analyzedAt":"2026-08-14T12:11:37.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}