alibaba/druid · error · DruidRuntimeException

dataSource inited.

Error message

dataSource inited.

What it means

DruidRuntimeException thrown by setCreateScheduler when the datasource has already been initialized (isInited() returns true). Scheduler executors are wired into the pool's lifecycle at init time; swapping one afterward would leave the running pool referencing the old executor, so Druid forbids it.

Source

Thrown at core/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java:2236

    public boolean isAsyncCloseConnectionEnable() {
        if (isRemoveAbandoned()) {
            return true;
        }
        return asyncCloseConnectionEnable;
    }

    public void setAsyncCloseConnectionEnable(boolean asyncCloseConnectionEnable) {
        this.asyncCloseConnectionEnable = asyncCloseConnectionEnable;
    }

    public ScheduledExecutorService getCreateScheduler() {
        return createScheduler;
    }

    public void setCreateScheduler(ScheduledExecutorService createScheduler) {
        if (isInited()) {
            throw new DruidRuntimeException("dataSource inited.");
        }
        this.createScheduler = createScheduler;
    }

    public ScheduledExecutorService getDestroyScheduler() {
        return destroyScheduler;
    }

    public void setDestroyScheduler(ScheduledExecutorService destroyScheduler) {
        if (isInited()) {
            throw new DruidRuntimeException("dataSource inited.");
        }
        this.destroyScheduler = destroyScheduler;
    }

    public boolean isInited() {
        return this.inited;
    }

View on GitHub (pinned to fa8dc99126)

Solutions

  1. Set createScheduler BEFORE init() / first getConnection() (in bean setup, before the pool starts).
  2. If the pool is already running, close() it, create a new DruidDataSource, set the scheduler, then init().
  3. Move scheduler bean injection to constructor/early init so it is applied during datasource construction.

Example fix

// before
dataSource.init();
dataSource.setCreateScheduler(myScheduler); // throws
// after
dataSource.setCreateScheduler(myScheduler);
dataSource.init();
Defensive patterns

Strategy: validation

Validate before calling

if (dataSource.isInited()) {
    throw new IllegalStateException("set createScheduler before init(); pool is already initialized");
}
dataSource.setCreateScheduler(scheduler);

Prevention

When it happens

Trigger: Calling dataSource.setCreateScheduler(...) after dataSource.init() has run, or after the first getConnection() triggered lazy init. The isInited() guard at line 2232 throws.

Common situations: Spring/guice bean wiring that injects a scheduler via a setter after the datasource bean is constructed and initialized; calling init() explicitly then reconfiguring; reusing a datasource instance and trying to swap schedulers.

Related errors


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