code: def sessions_scope(local_session, commit=False):
      
      try:
          yield local_session
          if commit:
              local_session.commit()
              logger.debug("DB session auto-committed as requested")
      except Exception as e:
          # We log the exception before re-raising it, in case the rollback also
          # fails
          logger.exception("Exception during scoped worker transaction, " "rolling back.")
          # This rollback is potentially redundant with the remove call below,
          # depending on how the scoped session is configured, but we'll be
          # explicit here.
          local_session.rollback()
          raise e
      finally:
          local_session.remove()
          logger.debug("Session complete, db session closed")
docs: Provide a transactional scope around a series of operations.