On Mon, Sep 6, 2010 at 7:12 PM, Patryk Zawadzki <pat...@pld-linux.org> wrote:
> It would be more useful if you could explicitly
> enter_isolation_block() and leave_isolation_block() as needed
> (currently there is no way to commit the isolating transaction other
> than doing a raw SQL query or accessing psycopg internals).

Another usecase I just came along:

Say I have to generate some unique string for the database. Something like that:

    potential = base = slugify(obj.bar)
    suffix = 0
    while True:
        if not Baz.objects.filter(foo=potential).exists():
            obj.foo = potential
            obj.save()
            break
        suffix += 1
        potential = '%s-%s' % (base, suffix)

Except it's possible that another process or thread commits an
identical object right between the call to exists() and the save().
What I'd really want to do is something closer to this:

    potential = base = slugify(obj.bar)
    suffix = 0
    found = False
    while not found:
        enter_isolation_block(ISOLATION_SERIALIZABLE)
        if not Baz.objects.filter(foo=potential).exists():
            obj.foo = potential
            obj.save()
            found = True
        leave_isolation_block()
        suffix += 1
        potential = '%s-%s' % (base, suffix)

Of course with new python versions we can get the extra sugar coating:

with db.isolation(ISOLATION_SERIALIZABLE):
    # ...

-- 
Patryk Zawadzki

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to