def METHOD_NAME(connection):
    nested = connection.begin_nested()
    yield nested
    # if the transaction is still associated, this means it has e.g. pending trigger events.
    transaction_associated = nested.connection._transaction == nested
    if transaction_associated:
        nested.rollback()