{"record":{"id":"156952840403eac9","repo":"alibaba/druid","slug":"can-not-restart-activecount-not-zero","errorCode":null,"errorMessage":"can not restart, activeCount not zero. {}","messagePattern":"can not restart, activeCount not zero\\. (.+?)","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/alibaba/druid/pool/DruidDataSource.java","lineNumber":440,"sourceCode":"        this.resetStatEnable = resetStatEnable;\n        if (dataSourceStat != null) {\n            dataSourceStat.setResetStatEnable(resetStatEnable);\n        }\n    }\n\n    public long getDiscardCount() {\n        return discardCount;\n    }\n\n    public void restart() throws SQLException {\n        this.restart(null);\n    }\n\n    public void restart(Properties properties) throws SQLException {\n        lock.lock();\n        try {\n            if (activeCount > 0) {\n                throw new SQLException(\"can not restart, activeCount not zero. \" + activeCount);\n            }\n            if (LOG.isInfoEnabled()) {\n                LOG.info(\"{dataSource-\" + this.getID() + \"} restart\");\n            }\n\n            this.close();\n            this.resetStat();\n            this.inited = false;\n            this.enable = true;\n            this.closed = false;\n\n            if (properties != null) {\n                DruidDataSourceUtils.configFromProperties(this, properties);\n            }\n        } finally {\n            lock.unlock();\n        }\n    }","sourceCodeStart":422,"sourceCodeEnd":458,"githubUrl":"https://github.com/alibaba/druid/blob/fa8dc9912637a2f729eef9f55356621fec18d40e/core/src/main/java/com/alibaba/druid/pool/DruidDataSource.java#L422-L458","documentation":"SQLException thrown by restart() / restart(Properties) when activeCount > 0, i.e. when connections are still checked out to application code. Restart closes and reinitializes the pool, which would orphan in-flight transactions, so Druid refuses to restart while any connection is in use.","triggerScenarios":"Calling dataSource.restart() while one or more connections borrowed via getConnection() have not been returned (activeCount > 0). The guard at line 439 throws.","commonSituations":"Restarting the datasource during request handling; a connection leak (unclosed Connection/Statement/ResultSet) keeps activeCount non-zero; long-running queries/transactions in flight; restart invoked from a management endpoint under load.","solutions":["Drain active connections first: stop incoming work, wait for activeCount to reach 0 (dataSource.getActiveCount()), then restart.","Fix connection leaks so borrowed connections are always returned.","If draining is not possible, close() the datasource instead of restart() and create a new one.","Monitor getActiveCount() and only call restart() once it reports 0."],"exampleFix":"// before\nwhile (dataSource.getActiveCount() > 0) { /* busy wait */ }\ndataSource.restart();\n// after\n// stop accepting work, then:\nlong deadline = System.currentTimeMillis() + 30_000;\nwhile (dataSource.getActiveCount() > 0 && System.currentTimeMillis() < deadline) {\n    Thread.sleep(200);\n}\nif (dataSource.getActiveCount() == 0) {\n    dataSource.restart();\n} else {\n    throw new IllegalStateException(\"cannot drain active connections\");\n}","handlingStrategy":"validation","validationCode":"if (dataSource.getActiveCount() > 0) {\n    throw new IllegalStateException(\"cannot restart: \" + dataSource.getActiveCount()\n        + \" active connections still borrowed; drain first\");\n}\ndataSource.restart();","typeGuard":null,"tryCatchPattern":"try {\n    dataSource.restart();\n} catch (SQLException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"can not restart\")) {\n        LOG.warn(\"restart blocked by active connections: {}\", e.getMessage());\n        // back off and retry after draining\n    }\n    throw e;\n}","preventionTips":["Stop new work and wait for activeCount to hit 0 before restart().","Fix connection leaks so borrowed connections always return.","Use close()+recreate if draining is not feasible."],"tags":["lifecycle","restart","connection-leak","pool-state"],"backgroundTag":null,"analyzedSha":"fa8dc9912637a2f729eef9f55356621fec18d40e","analyzedAt":"2026-08-14T04:55:06.789Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}