{"record":{"id":"5a04a54321e95916","repo":"brettwooldridge/HikariCP","slug":"metricregistry-can-only-be-set-one-time","errorCode":null,"errorMessage":"MetricRegistry can only be set one time","messagePattern":"MetricRegistry can only be set one time","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/zaxxer/hikari/HikariDataSource.java","lineNumber":241,"sourceCode":"\n      return false;\n   }\n\n   // ***********************************************************************\n   //                        HikariConfigMXBean methods\n   // ***********************************************************************\n\n   /** {@inheritDoc} */\n   @Override\n   public void setMetricRegistry(Object metricRegistry)\n   {\n      var isAlreadySet = getMetricRegistry() != null;\n      super.setMetricRegistry(metricRegistry);\n\n      var p = pool;\n      if (p != null) {\n         if (isAlreadySet) {\n            throw new IllegalStateException(\"MetricRegistry can only be set one time\");\n         }\n         else {\n            p.setMetricRegistry(super.getMetricRegistry());\n         }\n      }\n   }\n\n   /** {@inheritDoc} */\n   @Override\n   public void setMetricsTrackerFactory(MetricsTrackerFactory metricsTrackerFactory)\n   {\n      var isAlreadySet = getMetricsTrackerFactory() != null;\n      super.setMetricsTrackerFactory(metricsTrackerFactory);\n\n      var p = pool;\n      if (p != null) {\n         if (isAlreadySet) {\n            throw new IllegalStateException(\"MetricsTrackerFactory can only be set one time\");","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/brettwooldridge/HikariCP/blob/a4d93f4f85517f90e632b795486d7102e933d7ff/src/main/java/com/zaxxer/hikari/HikariDataSource.java#L223-L259","documentation":"setMetricRegistry() on HikariDataSource refuses to change the metric registry once the pool has been started and a registry was already set. HikariCP wires metrics into the live pool at startup, so re-assigning the registry afterwards would silently produce wrong metrics; it throws IllegalStateException instead.","triggerScenarios":"Calling setMetricRegistry(...) twice while the pool is running; a framework (Spring Boot metrics, Dropwizard) injecting the registry in a @PostConstruct that runs after the pool lazily started; reconfiguring metrics at runtime via JMX/actuator.","commonSituations":"Spring Boot actuator + custom MetricRegistry bean, micrometer migration re-binding registries, dev hot-reload re-running configuration against the same live DataSource.","solutions":["Set the metric registry once, before the first getConnection() (i.e., before pool initialization)","Remove duplicate configuration paths that set the registry (check both your code and framework autoconfiguration)","If you must change registries, close the DataSource, create a new one configured correctly, and swap it","Set spring.datasource.hikari.* properties (or the equivalent) declaratively so ordering is deterministic"],"exampleFix":"// before\nHikariDataSource ds = new HikariDataSource(config);\nds.getConnection(); // pool starts\nds.setMetricRegistry(registry); // second set -> IllegalStateException\n\n// after\nconfig.setMetricRegistry(registry); // before startup\nHikariDataSource ds = new HikariDataSource(config);","handlingStrategy":"validation","validationCode":"// set registry once, before pool start\nHikariConfig cfg = new HikariConfig();\ncfg.setMetricRegistry(registry);\n// assert getMetricRegistry() == null before calling setter on an existing DS\nif (ds.getMetricRegistry() != null) { /* skip: already bound */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Assign metric registries in one place, before first getConnection","Never rebind registries on a live pool; rebuild instead","Watch for framework autoconfig also setting the registry"],"tags":["hikaricp","metrics","configuration","lifecycle"],"backgroundTag":null,"analyzedSha":"a4d93f4f85517f90e632b795486d7102e933d7ff","analyzedAt":"2026-08-14T12:11:37.292Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}