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
- Verify network reachability and credentials for the target DB from the app host (telnet/nc to host:port, test login).
- Update the db_username/db_password/db_url in sys_data_source (datasource management UI) and clear the datasource cache.
- Ensure the correct JDBC driver jar is on the classpath (check pom dependencies for mysql/oracle/postgres).
- 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
- Test connectivity from the app host before saving a datasource.
- Keep the driver jar on the classpath.
- Clear the datasource cache after config changes.
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
- 数据源URL配置格式不正确!
- 表名不合法,存在SQL注入风险!--->{table}
- 字段不合法,存在SQL注入风险!--->{field}
- DaoFormat 是 minidao 保留关键字,不允许使用 ,请更改参数定义!
- 上传业务路径深度超出限制!
AI-assisted analysis of jeecgboot/JeecgBoot@96fb33f5ec (2026-08-14).
Data as JSON: /api/errors/abae508862273add.
Report an issue: GitHub.