使用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); } }
为什么每次启动都要读取表信息呢?如何优化?不需要每次都读取?