run<R> method
Runs action exclusively and rolls back any transaction left open.
Implementation
@override
Future<R> run<R>(Future<R> Function(SqlConnection) action) {
if (_closing != null) {
return Future.error(
const OrmException(
'DRIVER.CLOSED',
'MySQL driver is closing or closed.',
),
);
}
final result = _tail.then((_) async {
if (_discarded || !_connection.connected) {
throw const OrmException(
'DRIVER.CLOSED',
'MySQL connection has been discarded. Open a new driver.',
);
}
final lease = MysqlConnection(_connection, _options.queryTimeout);
try {
final result = await action(lease);
await lease.release();
return result;
} catch (error, stack) {
try {
await lease.release();
} catch (_) {
await lease.invalidate();
}
Error.throwWithStackTrace(error, stack);
} finally {
if (lease.isInvalid) _discarded = true;
}
});
_tail = result.then<void>((_) {}, onError: (Object _, StackTrace _) {});
return result;
}