> Den 10/02/2015 kl. 17.15 skrev Erik Cederstrand <erik+li...@cederstrand.dk>:
> 
> Hi list,
> 
> I'm tracking down a bug in my app that shouldn't be happening with the 
> transaction statements I added in my code. In my logging settings, I can set 
> 'django.db.backends' to DEBUG to log the queries (I'm using the postresql 
> backend). I see 'SAVEPOINT' statements logged, but I also need 'BEGIN' and 
> 'COMMIT' so I can see when the transaction started and ended. How do I do 
> that?

Hmm, I'm beginning to think I don't understand Django transactions. I have the 
following two snippets, boiled down from my original code. The first one calls 
cache methods via helper functions, the other one calls the cache methods 
directly. If I run the code in parallel in two different processes, then the 
first example asserts almost instantly, while the second example survives 
several minutes. Can someone explain this?

import random
from django.db import transaction
from django.core.cache import cache

def get_val(key):
    cache.get(key)

def set_val(key, val):
    cache.add(key, val)

def del_val(key):
    cache.delete(key)


# First example, fails
while True:
    with transaction.atomic():
        if not get_val('foo'):
            print('no key found')
            time.sleep(random.random())
            if set_val('foo', 'bar'):
                print('key added')
                time.sleep(random.random())
            else:
                assert False
            del_val('foo')
            print('key deleted')
    time.sleep(random.random())


# Second example, runs correctly
while True:
    with transaction.atomic():
        if not cache.get('foo'):
            print('no key found')
            time.sleep(random.random())
            if cache.add('foo', 'bar'):
                print('key added')
                time.sleep(random.random())
            else:
                assert False
            cache.delete('foo')
            print('key deleted')
    time.sleep(random.random())

Thanks,
Erik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/5D7F4F83-FFEB-4658-AF81-B6B94576ADBA%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to