use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method load.
/**
* @todo 加载单个对象
* @param sqlToyContext
* @param entity
* @param cascadeTypes
* @param lockMode
* @param dataSource
* @return
*/
public <T extends Serializable> T load(final SqlToyContext sqlToyContext, final T entity, final Class[] cascadeTypes, final LockMode lockMode, final DataSource dataSource) {
if (entity == null) {
logger.warn("load entity is null,please check!");
return null;
}
try {
// 单记录操作返回对应的库和表配置
final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, entity, false, dataSource);
SqlExecuteStat.start(BeanUtil.getEntityClass(entity.getClass()).getName(), "load", null);
return (T) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).load(sqlToyContext, entity, (cascadeTypes == null) ? null : CollectionUtil.arrayToList(cascadeTypes), lockMode, conn, dbType, dialect, shardingModel.getTableName()));
}
});
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler 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();
}
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method delete.
/**
* @todo 删除单个对象
* @param sqlToyContext
* @param entity
* @param dataSource
* @return
*/
public Long delete(final SqlToyContext sqlToyContext, final Serializable entity, final DataSource dataSource) {
if (entity == null) {
logger.warn("delete entity is null,please check!");
return 0L;
}
try {
SqlExecuteStat.start(BeanUtil.getEntityClass(entity.getClass()).getName(), "delete", null);
// 获取分库分表策略结果
final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, entity, false, dataSource);
Long updateTotalCnt = (Long) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).delete(sqlToyContext, entity, conn, dbType, dialect, shardingModel.getTableName()));
}
});
SqlExecuteStat.debug("执行结果", "删除操作影响记录量:{} 条!", updateTotalCnt);
return updateTotalCnt;
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method save.
/**
* @todo 保存单个对象记录
* @param sqlToyContext
* @param entity
* @param dataSource
* @return
*/
public Serializable save(final SqlToyContext sqlToyContext, final Serializable entity, final DataSource dataSource) {
if (entity == null) {
logger.warn("save entity is null,please check!");
return null;
}
try {
SqlExecuteStat.start(BeanUtil.getEntityClass(entity.getClass()).getName(), "save", null);
final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, entity, true, dataSource);
Serializable result = (Serializable) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).save(sqlToyContext, entity, conn, dbType, dialect, shardingModel.getTableName()));
}
});
SqlExecuteStat.debug("执行结果", "单对象保存返回主键值:{}", result);
return result;
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method updateSaveFetch.
/**
* @TODO 适用于库存台账、客户资金账强事务高并发场景,一次数据库交互实现:1、锁查询;2、记录存在则修改;3、记录不存在则执行insert;4、返回修改或插入的记录信息
* @param sqlToyContext
* @param entity
* @param updateRowHandler
* @param uniqueProps 空则表示根据主键查询
* @param dataSource
* @return
*/
public Serializable updateSaveFetch(final SqlToyContext sqlToyContext, final Serializable entity, final UpdateRowHandler updateRowHandler, final String[] uniqueProps, final DataSource dataSource) {
if (entity == null || updateRowHandler == null) {
logger.warn("updateSaveFetch entity or updateRowHandler is null,please check!");
return null;
}
try {
SqlExecuteStat.start(BeanUtil.getEntityClass(entity.getClass()).getName(), "updateSaveFetch", null);
final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, entity, false, dataSource);
Serializable result = (Serializable) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).updateSaveFetch(sqlToyContext, entity, updateRowHandler, uniqueProps, conn, dbType, dialect, shardingModel.getTableName()));
}
});
return result;
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
Aggregations