transaction<T> method

  1. @override
Future<T> transaction<T>(
  1. FutureOr<T> f(
    1. QueryExecutor
    )
)

Enters a database transaction, performing the actions within, and returning the results of f.

If f fails, the transaction will be rolled back, and the responsible exception will be re-thrown.

Whether nested transactions are supported depends on the underlying driver.

Implementation

@override
Future<T> transaction<T>(FutureOr<T> Function(QueryExecutor) f) async {
  //if (_connection is! PostgreSQLConnection) {
  //  return await f(this);
  //}

  var conn = _session as Connection;

  return await conn.runTx((session) async {
    try {
      //logger.fine('Entering transaction');
      var exec = PostgreSqlExecutor(session, logger: logger);
      return await f(exec);
    } catch (e) {
      session.rollback();
      //ctx.cancelTransaction(reason: e.toString());
      logger.warning("The transation has failed due to ", e);
      rethrow;
    }
  }).onError((error, stackTrace) {
    throw StateError('The transaction was cancelled.');
  });
}