use of org.sagacity.sqltoy.model.LockMode in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method loadAll.
/**
* @todo 批量加载集合(自4.13.1 版本已经自动将超大规模集合拆分执行),规避了jpa等框架的缺陷
* @param sqlToyContext
* @param entities
* @param cascadeTypes
* @param lockMode
* @param dataSource
* @return
*/
public <T extends Serializable> List<T> loadAll(final SqlToyContext sqlToyContext, final List<T> entities, final Class[] cascadeTypes, final LockMode lockMode, final DataSource dataSource) {
if (entities == null || entities.isEmpty()) {
logger.warn("loadAll entities is null or empty,please check!");
return entities;
}
try {
SqlExecuteStat.start(BeanUtil.getEntityClass(entities.get(0).getClass()).getName(), "loadAll:[" + entities.size() + "]条记录!", null);
// 一般in的最大数量是1000
int batchSize = SqlToyConstants.getLoadAllBatchSize();
// 对可能存在的配置参数定义错误进行校正,最大控制在1000内
if (batchSize > 1000 || batchSize < 1) {
batchSize = 1000;
}
int totalSize = entities.size();
int batch = (totalSize + batchSize - 1) / batchSize;
List result = new ArrayList();
List batchEntities;
for (int i = 0; i < batch; i++) {
// 切取单个批次的记录
batchEntities = entities.subList(i * batchSize, (i == batch - 1) ? totalSize : (i + 1) * batchSize);
// 分库分表并行执行,并返回结果
result.addAll(ParallelUtils.execute(sqlToyContext, batchEntities, false, dataSource, (context, batchModel) -> {
ShardingModel shardingModel = batchModel.getShardingModel();
return (List) DataSourceUtils.processDataSource(context, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).loadAll(context, batchModel.getEntities(), (cascadeTypes == null) ? null : CollectionUtil.arrayToList(cascadeTypes), lockMode, conn, dbType, dialect, shardingModel.getTableName(), getFetchSize(-1), -1));
}
});
}));
}
SqlExecuteStat.debug("执行结果", "查询结果记录:{} 条!", result.size());
return result;
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
Aggregations