run<R> method

  1. @override
Future<R> run<R>(
  1. Future<R> action(
    1. SqlConnection
    )
)
override

Leases a pooled connection for the lifetime of the callback future.

Implementation

@override
Future<R> run<R>(Future<R> Function(SqlConnection) action) async {
  if (_closed) {
    throw const OrmException('DRIVER.CLOSED', 'PostgreSQL driver is closed.');
  }
  var acquired = false;
  try {
    return await _pool.withConnection(
      (connection) {
        acquired = true;
        return action(
          PostgresConnection(
            connection,
            _cancelConnection,
            _backendIds,
            _queryTimeout,
          ),
        );
      },
      settings: _connectTimeout == null
          ? null
          : pg.ConnectionSettings(connectTimeout: _connectTimeout),
    );
  } on TimeoutException catch (error) {
    if (acquired) rethrow;
    throw OrmException(
      'CONNECTION.TIMEOUT',
      'PostgreSQL connection acquisition timed out.',
      cause: error,
    );
  }
}