def _lock_pk_with_retries()

in sapp/db_support.py [0:0]


    def _lock_pk_with_retries(self, session: Session, cls: Type) -> Optional[Type]:
        # pyre-fixme[24]: Generic type `type` expects 1 type parameter, use
        #  `typing.Type` to avoid runtime subscripting errors.
        cls_pk: Optional[Type] = None
        retries: int = 6
        while retries > 0:
            try:
                cls_pk = (
                    session.query(self.PRIMARY_KEY)
                    .filter(self.PRIMARY_KEY.table_name == cls.__name__)
                    .with_for_update()
                    .first()
                )
                # if we're here, the record has been locked, or there is no record
                retries = 0
            except exc.OperationalError as ex:
                # Failed to get exclusive lock on the record, so we retry
                retries -= 1
                # Re-raise the exception if our retries are exhausted
                if retries == 0:
                    raise ex
        return cls_pk