{"record":{"id":"38aff2b993d7df13","repo":"brettwooldridge/HikariCP","slug":"cannot-use-setmetricstrackerfactory-and-setmetri","errorCode":null,"errorMessage":"cannot use setMetricsTrackerFactory() and setMetricRegistry() together","messagePattern":"cannot use setMetricsTrackerFactory\\(\\) and setMetricRegistry\\(\\) together","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/zaxxer/hikari/HikariConfig.java","lineNumber":651,"sourceCode":"    * via {@link Connection#rollback()}.  Defaults to {@code false}.\n    *\n    * @param isolate {@code true} if internal pool queries should be isolated, {@code false} if not\n    */\n   public void setIsolateInternalQueries(boolean isolate)\n   {\n      checkIfSealed();\n      this.isIsolateInternalQueries = isolate;\n   }\n\n   public MetricsTrackerFactory getMetricsTrackerFactory()\n   {\n      return metricsTrackerFactory;\n   }\n\n   public void setMetricsTrackerFactory(MetricsTrackerFactory metricsTrackerFactory)\n   {\n      if (metricRegistry != null) {\n         throw new IllegalStateException(\"cannot use setMetricsTrackerFactory() and setMetricRegistry() together\");\n      }\n\n      this.metricsTrackerFactory = metricsTrackerFactory;\n   }\n\n   /**\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    *","sourceCodeStart":633,"sourceCodeEnd":669,"githubUrl":"https://github.com/brettwooldridge/HikariCP/blob/a4d93f4f85517f90e632b795486d7102e933d7ff/src/main/java/com/zaxxer/hikari/HikariConfig.java#L633-L669","documentation":"HikariCP supports two mutually exclusive ways to wire metrics: a MetricsTrackerFactory (custom programmatic SPI) or a MetricRegistry object (Dropwizard/metrics5/Micrometer registry). setMetricsTrackerFactory() throws IllegalStateException if a metricRegistry has already been set, because there would be no unambiguous destination for metrics. The check is one-directional here — setMetricRegistry() performs the mirror-image check.","triggerScenarios":"Calling config.setMetricRegistry(registry) followed by config.setMetricsTrackerFactory(factory) (or the equivalent properties/micrometer autoconfiguration sequence). Common in Spring Boot apps where a Micrometer registry is auto-configured and application code then also installs a custom MetricsTrackerFactory on the same HikariConfig.","commonSituations":"Spring Boot with micrometer-core on the classpath auto-setting the registry, then custom code adding its own tracker factory; migrating metrics stacks (Dropwizard -> Micrometer) while old config remains; shared/copied HikariConfig objects receiving both settings from different modules.","solutions":["Choose one mechanism: pass the Micrometer/Dropwizard registry OR a custom MetricsTrackerFactory, never both","In Spring Boot, prefer letting Boot's autoconfiguration manage Hikari metrics via Micrometer instead of setting a factory manually","Audit the config assembly path (properties files, profile overlays, builders) for both keys being set","If you truly need custom behavior, implement it inside a MetricsTrackerFactory and drop the registry setting"],"exampleFix":"// before\nconfig.setMetricRegistry(micrometerRegistry);\nconfig.setMetricsTrackerFactory(customFactory); // IllegalStateException\n\n// after (pick ONE)\nconfig.setMetricsTrackerFactory(customFactory);\n// or\nconfig.setMetricRegistry(micrometerRegistry);","handlingStrategy":"validation","validationCode":"if (config.getMetricRegistry() == null) {\n   config.setMetricsTrackerFactory(factory);\n} // else: registry already chosen, skip the factory","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide the metrics backend once at the architecture level; never mix registry and factory","In Spring Boot, let autoconfiguration own Hikari metrics and add custom metrics via Micrometer instead","Guard each metrics setter with a null check of the alternate property"],"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"}