withTransaction method

  1. @override
Future<DbResult<void>> withTransaction(
  1. Future<String> operate(
    1. DbSession session
    ), {
  2. DbTransactionOptions? transactionOptions,
  3. void onmessage({
    1. String? err,
    2. String? msg,
    3. String? warn,
    })?,
})
override

事务批量操作

  • operate 批量操作回调函数。在回调中未抛出异常则提交事务,抛出异常则回滚事务,务必将回调参数session赋值给回调中的每个api的xxxxOptions.session
  • 注意:底层的mongo数据库驱动未实现对事务的支持,所以当驱动类型为EasyUniDbDriver.mongo时,本接口只是一个普通的批量回调操作

Implementation

@override
Future<DbResult<void>> withTransaction(Future<String> Function(DbSession session) operate, {DbTransactionOptions? transactionOptions, void Function({String? msg, String? warn, String? err})? onmessage}) async {
  try {
    final result = await _handle.withTransaction(operate, transactionOptions: transactionOptions, onmessage: onmessage ?? ({msg, warn, err}) => _defaultTransactionMessageListener(this, msg: msg, warn: warn, err: err));
    (result.success ? logDebug : logWarn)(['withTransaction =>', transactionOptions, result]);
    return result;
  } catch (error, stack) {
    logError(['withTransaction =>', transactionOptions, error, '\n', stack]);
    return DbResult(success: false, rescode: -1, message: error.toString());
  }
}