jd-opensource/joyagent-jdgenie · critical · RuntimeException

创建数据源失败

Error message

创建数据源失败

What it means

JdbcConnectionPools.createNewDatasource wraps any exception from jdbcConnectionPoolFactory.createPooledDatasource into RuntimeException '创建数据源失败' after logging the config. It means the connection pool could not be built — typically bad JDBC config, wrong driver, or an unsupported dialect thrown by the factory.

Solutions

  1. Read the logged cause and the original config to find the underlying failure.
  2. Verify the JDBC URL format and that the driver dependency is on the classpath.
  3. Confirm the dialect is MYSQL/H2/CLICKHOUSE (else the factory throws 暂不支持).
  4. Test credentials against the database directly.
  5. Validate the JdbcConnectionConfig fields before pool creation.

Example fix

// before
config.setUrl("jdbc:mysql://bad-host:3306/db"); // creation fails -> RuntimeException
// after
config.setUrl("jdbc:mysql://localhost:3306/mydb?useSSL=false"); // valid URL + driver present
Defensive patterns

Strategy: try-catch

Validate before calling

// Java: validate config before pool creation
Objects.requireNonNull(config.getUrl(), "jdbc url required");
Objects.requireNonNull(config.getUsername(), "username required");
if (!config.getUrl().startsWith("jdbc:")) {
    throw new IllegalArgumentException("invalid jdbc url: " + config.getUrl());
}

Try / catch

try {
    DatasourceWrapper ds = pools.getOrCreateConnectionPool(config);
} catch (RuntimeException e) {
    if ("创建数据源失败".equals(e.getMessage())) {
        log.error("pool creation failed; cause={} config={}", e.getCause(), config, e);
    }
    throw e;
}

Prevention

When it happens

Trigger: getOrCreateConnectionPool or newWrapper requesting a datasource whose creation fails: invalid JDBC URL, missing JDBC driver on classpath, wrong credentials, or an unsupported dialect from JdbcConnectionPoolFactory.

Common situations: Missing mysql-connector/clickhouse-jdbc dependency, malformed JDBC URL (bad host/port/params), password with unescaped special characters, or dialect not handled by the factory.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of jd-opensource/joyagent-jdgenie@2417e0b8b6 (2026-09-08). Data as JSON: /api/errors/cb0cfe5212d79676. Report an issue: GitHub.

Appendix: source

Thrown at genie-backend/src/main/java/com/jd/genie/data/jdbc/connection/JdbcConnectionPools.java:110


    private DatasourceWrapper refreshDatasource(JdbcConnectionConfig config) {
        String poolId = config.getKey();

        // 创建新的数据源
        DatasourceWrapper newWrapper = createNewDatasource(config);
        pools.put(poolId, newWrapper);

        log.info("数据源刷新完成 poolId {}", poolId);
        return newWrapper;
    }

    private DatasourceWrapper createNewDatasource(JdbcConnectionConfig config) {
        try {
            return jdbcConnectionPoolFactory.createPooledDatasource(config);
        } catch (Exception e) {
            log.error("创建数据源失败, config: {}", config, e);
            throw new RuntimeException("创建数据源失败", e);
        }
    }
}

View on GitHub (pinned to 2417e0b8b6)