transaction<T> method

  1. @override
Future<T> transaction<T>(
  1. ManagedContext transactionContext,
  2. Future<T> transactionBlock(
    1. ManagedContext transaction
    )
)
override

Implementation

@override
Future<T> transaction<T>(ManagedContext transactionContext,
    Future<T> transactionBlock(ManagedContext transaction)) async {
  final dbConnection = await getDatabaseConnection();

  T? output;
  Rollback? rollback;
  try {
    await dbConnection.transaction((dbTransactionContext) async {
      transactionContext.persistentStore =
          PostgreSQLPersistentStore._transactionProxy(
              this, dbTransactionContext);

      try {
        output = await transactionBlock(transactionContext);
      } on Rollback catch (e) {
        rollback = e;
        dbTransactionContext.cancelTransaction(reason: rollback!.reason);
      }
    });
  } on PostgreSQLException catch (e) {
    final QueryException<PostgreSQLException>? interpreted =
        _interpretException(e);
    if (interpreted != null) {
      throw interpreted;
    }

    rethrow;
  }

  if (rollback != null) {
    throw rollback as Object;
  }

  return output!;
}