transaction<T> method
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 f(this);
//print('PostgreSqlExecutor transaction');
var conn = connection as PostgreSQLConnection;
T? returnValue;
var txResult = await conn.transaction((ctx) async {
//print('PostgreSqlExecutor entering transaction');
try {
logger?.fine('Entering transaction');
var tx =
PostgreSqlExecutor(connectionInfo, logger: logger, connection: ctx);
returnValue = await f(tx);
// print('PostgreSqlExecutor end transaction');
} catch (e) {
// print('PostgreSqlExecutor catch transaction');
ctx.cancelTransaction(reason: e.toString());
rethrow;
} finally {
logger?.fine('Exiting transaction');
// print('PostgreSqlExecutor Exiting transactionn');
}
});
if (txResult is PostgreSQLRollback) {
/*if (txResult.reason == null) {
throw StateError('The transaction was cancelled.');
} else {*/
throw StateError(
'The transaction was cancelled with reason "${txResult.reason}".');
//}
} else {
return returnValue;
}
}