如何配置SchemaCrawlerAPI以同时支持多个JDBC驱动?
解决SchemaCrawler同时使用多个第三方JDBC驱动的问题
你的方法存在遗漏——SchemaCrawler本身支持同时处理多个第三方JDBC驱动,问题出在你全局设置了SC_WITHOUT_DATABASE_PLUGIN环境变量/系统属性,导致该配置对所有数据库操作生效,无法区分不同DBMS。
错误日志中文翻译
2025年1月21日 上午12:40:49 schemacrawler.tools.utility.SchemaCrawlerUtility buildSchemaRetrievalOptions 信息:未使用任何SchemaCrawler数据库插件 Exception in thread "main" schemacrawler.schemacrawler.exceptions.InternalRuntimeException: 请为<jdbc:postgresql://localhost:5432/postgres>将<postgresql>对应的SchemaCrawler数据库插件添加到CLASSPATH中, 或者设置环境变量或Java系统属性"SC_WITHOUT_DATABASE_PLUGIN=postgresql" at schemacrawler.tools.utility.SchemaCrawlerUtility.useMatchedDatabasePlugin(SchemaCrawlerUtility.java:297) at schemacrawler.tools.utility.SchemaCrawlerUtility.buildSchemaRetrievalOptions(SchemaCrawlerUtility.java:203) at schemacrawler.tools.utility.SchemaCrawlerUtility.matchSchemaRetrievalOptions(SchemaCrawlerUtility.java:145) at schemacrawler.tools.utility.SchemaCrawlerUtility.getCatalog(SchemaCrawlerUtility.java:78) at org.saakostya.schemaweaver.schemaexplorer.JdbcDriverLoader.printTablesWithSchemaCrawler(JdbcDriverLoader.java:121) at org.saakostya.schemaweaver.schemaexplorer.JdbcDriverLoader.main(JdbcDriverLoader.java:80) FAILURE: Build failed with an exception.
正确处理方案
不要全局设置SC_WITHOUT_DATABASE_PLUGIN,而是针对每个数据库操作单独配置,有两种可行方式:
方案1:针对单次操作临时设置系统属性
在执行某数据库的SchemaCrawler操作前,临时设置对应DBMS的属性,操作完成后恢复原配置,避免影响其他数据库处理。示例代码:
// 保存原属性值 String originalPluginProp = System.getProperty("SC_WITHOUT_DATABASE_PLUGIN"); try { // 针对PostgreSQL设置属性 System.setProperty("SC_WITHOUT_DATABASE_PLUGIN", "postgresql"); // 执行PostgreSQL的SchemaCrawler操作 executeSchemaCrawlerForPostgreSQL(); } finally { // 恢复原属性 if (originalPluginProp != null) { System.setProperty("SC_WITHOUT_DATABASE_PLUGIN", originalPluginProp); } else { System.clearProperty("SC_WITHOUT_DATABASE_PLUGIN"); } } // 处理MySQL数据库 try { System.setProperty("SC_WITHOUT_DATABASE_PLUGIN", "mysql"); executeSchemaCrawlerForMySQL(); } finally { // 恢复原属性 if (originalPluginProp != null) { System.setProperty("SC_WITHOUT_DATABASE_PLUGIN", originalPluginProp); } else { System.clearProperty("SC_WITHOUT_DATABASE_PLUGIN"); } }
并发场景下可结合ThreadLocal存储线程专属的属性设置,避免线程间干扰。
方案2:通过SchemaCrawler API直接配置跳过插件检查
不依赖全局属性,直接在构建SchemaRetrievalOptions时设置跳过插件匹配,每个数据库操作独立配置:
// 创建SchemaRetrievalOptions并跳过插件检查 SchemaRetrievalOptions schemaRetrievalOptions = new SchemaRetrievalOptions(); schemaRetrievalOptions.setSkipDatabasePlugin(true); // 构建SchemaCrawler配置选项 SchemaCrawlerOptions schemaCrawlerOptions = new SchemaCrawlerOptions(); // 配置其他需求选项... // 传入自定义配置获取Catalog Catalog catalog = SchemaCrawlerUtility.getCatalog(connection, schemaRetrievalOptions, schemaCrawlerOptions);
补充说明
SchemaCrawler要求设置SC_WITHOUT_DATABASE_PLUGIN是因为找不到对应DBMS的原生插件,跳过插件检查后,它会使用通用JDBC逻辑读取元数据,该逻辑对大多数第三方驱动均有效。
内容的提问来源于stack exchange,提问作者Konstantin
相关产品推荐
相关产品推荐

