ActiveRecordPlugin.start启动读取数据库慢

使用DruidPluginl连接数据正常,当进行ActiveRecordPlugin.start时,就比较慢了,因为这个方式会读取数据库相关表信息:

public boolean start() {
		if (isStarted) {
			return true;
		}
		if (config.dataSource == null && dataSourceProvider != null) {
			config.dataSource = dataSourceProvider.getDataSource();
		}
		if (config.dataSource == null) {
			throw new RuntimeException("ActiveRecord start error: ActiveRecordPlugin need DataSource or DataSourceProvider");
		}

		if (autoConfigDialect) {
			if (dataSourceProvider == null) {
				throw new RuntimeException("ActiveRecord start error: autoConfigDialect need DataSourceProvider");
			}
			autoConfigDialect(dataSourceProvider.getJdbcUrl());
		}

		config.sqlKit.parseSqlTemplate();

		tableBuilder.build(tableList, config);
		DbKit.addConfig(config);
		isStarted = true;
		return true;
}
tableBuilder.build(tableList, config);
public void build(List<Table> tableList, Config config) {
		// 支持 useAsDataTransfer(...) 中的 arp.start() 正常运作
		if (config.dataSource instanceof NullDataSource) {
			return ;
		}
		
		Table temp = null;
		Connection conn = null;
		try {
			conn = config.dataSource.getConnection();
			TableMapping tableMapping = TableMapping.me();
			for (Table table : tableList) {
				temp = table;
				doBuild(table, conn, config);
				tableMapping.putTable(table);
				DbKit.addModelToConfigMapping(table.getModelClass(), config);
			}
		} catch (Exception e) {
			if (temp != null) {
				System.err.println("Can not create Table object, maybe the table " + temp.getName() + " is not exists.");
			}
			throw new ActiveRecordException(e);
		}
		finally {
			config.close(conn);
		}
	}

为什么每次启动都要读取表信息呢?如何优化?不需要每次都读取?

评论区

JFinal

2025-04-14 15:16

这里确实有优化空间,我新做的 aifei 的数据库功能已经不用读数据库了,后面换我的新作吧

杜福忠

2025-04-14 17:22

我之前分享的https://jfinal.com/share/2768
可以绕过启动 JF 时扫表信息,提速非常明显,300张表映射时间降至1毫秒加载

北流家园网

2025-04-15 11:12

@杜福忠 大师一出手,又解决了我的问题,非常感谢杜总。

北流家园网

2025-04-15 11:12

@JFinal 旧系统无法切换呀,如果能平滑升级就好了

热门反馈

扫码入社