transaction<T> method
Runs work inside a database transaction.
Implementations commit when action completes and roll back when it
throws. Nested transactions and savepoints are not part of the shared
contract; drivers may reject or define their own behavior for them.
Implementation
@override
Future<T> transaction<T>(
Future<T> Function(AnySqlTransaction transaction) action,
) async {
_checkOpen();
await connection.execute(pg.Sql('BEGIN'));
final transaction = _PostgresAnySqlTransaction(connection);
try {
final value = await action(transaction);
if (!transaction.isCompleted) {
await transaction.commit();
}
return value;
} catch (_) {
if (!transaction.isCompleted) {
await transaction.rollback();
}
rethrow;
}
}