jeecgboot/JeecgBoot · error · JeecgBootException

动态数据源连接失败,dbKey:{dbKey}

Error message

动态数据源连接失败,dbKey:{dbKey}

What it means

Thrown by DynamicDBUtil.getDbSourceBydbKey when getJdbcDataSource returns a datasource that is null or not enabled (dataSource.isEnable() == false) — meaning Druid could not initialize a working pool for that dbKey within the 6-second maxWait. It surfaces as a JeecgBootException after the cache lookup and pool creation step.

Source

Thrown at jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/dynamic/db/DynamicDBUtil.java:97

     */
    public static DruidDataSource getDbSourceByDbKey(final String dbKey) {
        //获取多数据源配置
        DynamicDataSourceModel dbSource = DataSourceCachePool.getCacheDynamicDataSourceModel(dbKey);
        //先判断缓存中是否存在数据库链接
        DruidDataSource cacheDbSource = DataSourceCachePool.getCacheBasicDataSource(dbKey);
        if (cacheDbSource != null && !cacheDbSource.isClosed()) {
            log.debug("--------getDbSourceBydbKey------------------从缓存中获取DB连接-------------------");
            return cacheDbSource;
        } else {
            DruidDataSource dataSource = getJdbcDataSource(dbSource);
            if(dataSource!=null && dataSource.isEnable()){

                // 【TV360X-2060】设置超时时间 6秒
                dataSource.setMaxWait(6000);

                DataSourceCachePool.putCacheBasicDataSource(dbKey, dataSource);
            }else{
                throw new JeecgBootException("动态数据源连接失败,dbKey:"+dbKey);
            }
            log.info("--------getDbSourceBydbKey------------------创建DB数据库连接-------------------");
            return dataSource;
        }
    }

    /**
     * 关闭数据库连接池
     *
     * @param dbKey
     * @return
     */
    public static void closeDbKey(final String dbKey) {
        DruidDataSource dataSource = getDbSourceByDbKey(dbKey);
        try {
            if (dataSource != null && !dataSource.isClosed()) {
                dataSource.getConnection().commit();
                dataSource.getConnection().close();

View on GitHub (pinned to 96fb33f5ec)

Solutions

  1. Verify network reachability and credentials for the target DB from the app host (telnet/nc to host:port, test login).
  2. Update the db_username/db_password/db_url in sys_data_source (datasource management UI) and clear the datasource cache.
  3. Ensure the correct JDBC driver jar is on the classpath (check pom dependencies for mysql/oracle/postgres).
  4. Inspect app logs for the underlying Druid SQLException to see the real cause (auth refused, unknown host, etc.).

Example fix

// before
// dbKey 'erp' points at unreachable host -> JeecgBootException
DynamicDBUtil.getDbSourceBydbKey("erp");

// after (fix config + clear cache)
DataSourceCachePool.removeCacheBasicDataSource("erp");
// then correct sys_data_source row and retry
Defensive patterns

Strategy: retry

Validate before calling

// pre-flight connectivity check
try (Connection c = DriverManager.getConnection(url, user, pwd)) { /* ok */ }
catch (SQLException e) { /* do not cache, alert admin */ }

Type guard

public static boolean datasourceReachable(DynamicDataSourceModel m){
    try (var c = DriverManager.getConnection(m.getDbUrl(), m.getDbUsername(), m.getDbPassword())) { return c.isValid(3); }
    catch (Exception e) { return false; }
}

Try / catch

try { DynamicDBUtil.getDbSourceBydbKey(dbKey); }
catch (JeecgBootException e) { DataSourceCachePool.removeCacheBasicDataSource(dbKey); throw e; }

Prevention

When it happens

Trigger: Dynamic datasource unreachable (wrong host/port/credentials), the DB is down, network/firewall blocks the connection, the driver class is missing from the classpath so the pool cannot initialize, or credentials in sys_data_source are stale.

Common situations: Switching environments without updating datasource records; DB migrated to a new host; password rotated in the target DB but not in sys_data_source; a connection blip during heavy load; missing ojdbc/mysql driver jar in the deployed artifact.

Related errors


AI-assisted analysis of jeecgboot/JeecgBoot@96fb33f5ec (2026-08-14). Data as JSON: /api/errors/abae508862273add. Report an issue: GitHub.