Hi,

> Let's say I have a model with an integer value. I'd like to increment
> that value.
>
> Multiple processes would like to increment that value.
>
> Does the transaction middleware properly protect against this?
>
> Example:
> Processes P1, P2, model instance m, with m.val
>
> - P1 grabs the object : m = MyModel.objects.filter(get the m i want)
> [0]
> - P2 grabs the object : m = MyModel.objects.filter(get the m i want)
> [0]
> - P1 m.val += 1
> - P2 m.val += 1
> - P1 m.save()
> - P2 m.save()
>
> What happens normally? What happens with the transaction middleware?

The transaction middleware can't do anything special in these
situations. The instances of 'm' in processes P1 and P2 are two
separate Python object instances. If P2's m.save() gets called after
P1's m.save(), then m.val from P2's instance will get saved in the
database regardless of the m.val set by P1. The two instances are not
connected to each other.

>
> What happens if the view is more complicated and only conditionally
> accesses certain models. Will all models actually accessed be properly
> locked?

No. The model instances are not locked like this at all.

You will need a global shared memory/storage area to simulate an
interprocess lock of your own. Realize that your processes could even
be on different web servers. For example, you could use a "slot" in
memcache to acquire and release a custom lock every time around the
statements where you increment m.val. Or you could possibly have a
lock management table/model in your database.

-Rajesh D

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

Reply via email to