alibaba/druid · warning · IllegalStateException

getStatData error

Error message

getStatData error

What it means

Thrown by getStatDataAndIdentities() (the JMX-backed stat accessor) when reading JMX attribute values raises a JMException. Druid collects ~45 monitoring attributes from its JMX beans; if any bean/attribute access fails it wraps the failure as IllegalStateException("getStatData error", ex) so callers get a single typed exception rather than JMException leaking.

Source

Thrown at core/src/main/java/com/alibaba/druid/pool/DruidDataSource.java:3599

            map.put("PreparedStatementCacheAccessCount", this.getCachedPreparedStatementAccessCount());
            map.put("PreparedStatementCacheMissCount", this.getCachedPreparedStatementMissCount());
            map.put("PreparedStatementCacheHitCount", this.getCachedPreparedStatementHitCount());
            map.put("PreparedStatementCacheCurrentCount", this.getCachedPreparedStatementCount());
            map.put("Version", this.getVersion());

            // 40 -
            map.put("LastErrorTime", this.getLastErrorTime());
            map.put("LastCreateErrorTime", this.getLastCreateErrorTime());
            map.put("CreateErrorCount", this.getCreateErrorCount());
            map.put("DiscardCount", this.getDiscardCount());
            map.put("ExecuteQueryCount", this.getExecuteQueryCount());

            map.put("ExecuteUpdateCount", this.getExecuteUpdateCount());
            map.put("InitStackTrace", this.getInitStackTrace());

            return map;
        } catch (JMException ex) {
            throw new IllegalStateException("getStatData error", ex);
        }
    }

    public Map<String, Object> getStatData() {
        final int activeCount;
        final int activePeak;
        final Date activePeakTime;

        final int poolingCount;
        final int poolingPeak;
        final Date poolingPeakTime;

        final long connectCount;
        final long closeCount;

        lock.lock();
        try {
            poolingCount = this.poolingCount;

View on GitHub (pinned to fa8dc99126)

Solutions

  1. Read the chained JMException (getCause()) to find which attribute access failed and fix that underlying getter/null.
  2. Avoid scraping stats while the DataSource is being closed/init; guard monitoring calls with a started && !closed check.
  3. If using a monitoring integration, ensure it tolerates IllegalStateException and backs off rather than spamming.
  4. Upgrade Druid if this recurs — several attribute-serialisation NPEs were fixed across versions.

Example fix

// before — bare stat read blows up at shutdown
Map<String,Object> m = dataSource.getStatDataAndIdentities();

// after — guard against pool lifecycle races
if (!dataSource.isInited() || dataSource.isClosed()) {
    return Collections.emptyMap();
}
try {
    return dataSource.getStatDataAndIdentities();
} catch (IllegalStateException e) {
    log.warn("stat collection failed: {}", e.getCause().getMessage());
    return Collections.emptyMap();
}
Defensive patterns

Strategy: try-catch

Validate before calling

// guard stat scraping against lifecycle races
if (!dataSource.isInited() || ((DruidDataSource) dataSource).isClosed()) {
    return Collections.emptyMap();
}
return dataSource.getStatDataAndIdentities();

Try / catch

try {
    return dataSource.getStatDataAndIdentities();
} catch (IllegalStateException e) {
    // chained JMException names the failing attribute
    log.warn("stat read failed: {}", e.getCause() == null ? e : e.getCause().getMessage());
    return Collections.emptyMap();
}

Prevention

When it happens

Trigger: Calling dataSource.getStatDataAndIdentities() while an internal JMX MBean is in an inconsistent state, or an attribute getter throws (e.g. JdbcUtils/JMXUtils serialising a null LastError via getErrorCompositeData). The catch at line 3598 wraps the JMException.

Common situations: Concurrent close() racing with stat collection; a custom exceptionSorter/filter corrupting lastError; JMX security manager denying attribute access; an attribute returning a type JMXUtils cannot serialise; monitoring agent (Prometheus exporter, Druid stat JSON) scraping during shutdown.

Related errors


AI-assisted analysis of alibaba/druid@fa8dc99126 (2026-08-14). Data as JSON: /api/errors/6a23719bc9bc2347. Report an issue: GitHub.