diff --git a/packit_service/models.py b/packit_service/models.py index 4058f305b..7fc441b81 100644 --- a/packit_service/models.py +++ b/packit_service/models.py @@ -2341,33 +2341,31 @@ def add_scan_transaction(self) -> Generator["OSHScanModel"]: raise: IntegrityError if the scan model already exists """ - session = singleton_session or Session() - session.begin() - - try: - scan = OSHScanModel() - scan.copr_build_target = self - session.add(scan) - session.commit() - except Exception as ex: - logger.warning(f"Exception while working with database: {ex!r}") - session.rollback() - raise - - try: - yield scan - except Exception as ex: - logger.warning(f"{ex!r}") - session.rollback() - raise - - try: - session.add(scan) - session.commit() - except Exception as ex: - logger.warning(f"Exception while working with database: {ex!r}") - session.rollback() - raise + with sa_session_transaction() as session: + try: + scan = OSHScanModel() + scan.copr_build_target = self + session.add(scan) + session.commit() + except Exception as ex: + logger.warning(f"Exception while working with database: {ex!r}") + session.rollback() + raise + + try: + yield scan + except Exception as ex: + logger.warning(f"{ex!r}") + session.rollback() + raise + + try: + session.add(scan) + session.commit() + except Exception as ex: + logger.warning(f"Exception while working with database: {ex!r}") + session.rollback() + raise class KojiBuildGroupModel(ProjectAndEventsConnector, GroupModel, Base):