{"record":{"id":"abae508862273add","repo":"jeecgboot/JeecgBoot","slug":"dbkey-dbkey","errorCode":null,"errorMessage":"动态数据源连接失败，dbKey：{dbKey}","messagePattern":"动态数据源连接失败，dbKey：(.+?)","errorType":"exception","errorClass":"JeecgBootException","httpStatus":null,"severity":"error","filePath":"jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/dynamic/db/DynamicDBUtil.java","lineNumber":97,"sourceCode":"     */\n    public static DruidDataSource getDbSourceByDbKey(final String dbKey) {\n        //获取多数据源配置\n        DynamicDataSourceModel dbSource = DataSourceCachePool.getCacheDynamicDataSourceModel(dbKey);\n        //先判断缓存中是否存在数据库链接\n        DruidDataSource cacheDbSource = DataSourceCachePool.getCacheBasicDataSource(dbKey);\n        if (cacheDbSource != null && !cacheDbSource.isClosed()) {\n            log.debug(\"--------getDbSourceBydbKey------------------从缓存中获取DB连接-------------------\");\n            return cacheDbSource;\n        } else {\n            DruidDataSource dataSource = getJdbcDataSource(dbSource);\n            if(dataSource!=null && dataSource.isEnable()){\n\n                // 【TV360X-2060】设置超时时间 6秒\n                dataSource.setMaxWait(6000);\n\n                DataSourceCachePool.putCacheBasicDataSource(dbKey, dataSource);\n            }else{\n                throw new JeecgBootException(\"动态数据源连接失败，dbKey：\"+dbKey);\n            }\n            log.info(\"--------getDbSourceBydbKey------------------创建DB数据库连接-------------------\");\n            return dataSource;\n        }\n    }\n\n    /**\n     * 关闭数据库连接池\n     *\n     * @param dbKey\n     * @return\n     */\n    public static void closeDbKey(final String dbKey) {\n        DruidDataSource dataSource = getDbSourceByDbKey(dbKey);\n        try {\n            if (dataSource != null && !dataSource.isClosed()) {\n                dataSource.getConnection().commit();\n                dataSource.getConnection().close();","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/jeecgboot/JeecgBoot/blob/96fb33f5ec68516da0b0147da06b2eb0419e063a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/common/util/dynamic/db/DynamicDBUtil.java#L79-L115","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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.)."],"exampleFix":"// before\n// dbKey 'erp' points at unreachable host -> JeecgBootException\nDynamicDBUtil.getDbSourceBydbKey(\"erp\");\n\n// after (fix config + clear cache)\nDataSourceCachePool.removeCacheBasicDataSource(\"erp\");\n// then correct sys_data_source row and retry","handlingStrategy":"retry","validationCode":"// pre-flight connectivity check\ntry (Connection c = DriverManager.getConnection(url, user, pwd)) { /* ok */ }\ncatch (SQLException e) { /* do not cache, alert admin */ }","typeGuard":"public static boolean datasourceReachable(DynamicDataSourceModel m){\n    try (var c = DriverManager.getConnection(m.getDbUrl(), m.getDbUsername(), m.getDbPassword())) { return c.isValid(3); }\n    catch (Exception e) { return false; }\n}","tryCatchPattern":"try { DynamicDBUtil.getDbSourceBydbKey(dbKey); }\ncatch (JeecgBootException e) { DataSourceCachePool.removeCacheBasicDataSource(dbKey); throw e; }","preventionTips":["Test connectivity from the app host before saving a datasource.","Keep the driver jar on the classpath.","Clear the datasource cache after config changes."],"tags":["datasource","connection-pool","configuration","database","jeecg-boot"],"backgroundTag":null,"analyzedSha":"96fb33f5ec68516da0b0147da06b2eb0419e063a","analyzedAt":"2026-08-14T00:04:16.786Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}