transaction<T> method
Future<T?>
transaction<T>(
- ManagedContext transactionContext,
- Future<
T?> transactionBlock(- 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) {
/// user triggered a manual rollback.
/// TODO: there is currently no reliable way for a user to detect
/// that a manual rollback occured.
/// The documented method of checking the return value from this method
/// does not work.
rollback = e;
dbTransactionContext.cancelTransaction(reason: rollback!.reason);
}
});
} on PostgreSQLException catch (e) {
final interpreted = _interpretException(e);
if (interpreted != null) {
throw interpreted;
}
rethrow;
}
if (rollback != null) {
throw rollback!;
}
return output;
}