def retry_on_integrity_error(self):
        """Re-raise :class:`~sqlalchemy.exc.IntegrityError` as `DBSerializationError`.

        This is mainly useful to handle race conditions in atomic
        blocks. For example, even if prior to a database INSERT we
        have verified that there is no existing row with the given
        primary key, we still may get an
        :class:`~sqlalchemy.exc.IntegrityError` if another transaction
        inserted a row with this primary key in the meantime. But if
        we do (within an atomic block)::

          with db.retry_on_integrity_error():
              db.session.add(instance)

        then if the before-mentioned race condition occurs,
        `DBSerializationError` will be raised instead of
        :class:`~sqlalchemy.exc.IntegrityError`, so that the
        transaction will be retried (by the atomic block), and the
        second time our prior-to-INSERT check will correctly detect a
        primary key collision.

        Note: :meth:`retry_on_integrity_error` triggers a session
        flush.

        """

        session = self.session
        assert session.info.get(_ATOMIC_FLAG_SESSION_INFO_KEY), \
            'Calls to "retry_on_integrity_error" must be wrapped in atomic block.'
        session.flush()
        try:
            yield
            session.flush()
        except IntegrityError:
            raise DBSerializationError
Re-raise :class:`~sqlalchemy.exc.IntegrityError` as `DBSerializationError`.

        This is mainly useful to handle race conditions in atomic
        blocks. For example, even if prior to a database INSERT we
        have verified that there is no existing row with the given
        primary key, we still may get an
        :class:`~sqlalchemy.exc.IntegrityError` if another transaction
        inserted a row with this primary key in the meantime. But if
        we do (within an atomic block)::

          with db.retry_on_integrity_error():
              db.session.add(instance)

        then if the before-mentioned race condition occurs,
        `DBSerializationError` will be raised instead of
        :class:`~sqlalchemy.exc.IntegrityError`, so that the
        transaction will be retried (by the atomic block), and the
        second time our prior-to-INSERT check will correctly detect a
        primary key collision.

        Note: :meth:`retry_on_integrity_error` triggers a session
        flush.