On Nov 9, 2009, at 8:36 PM, Continuation wrote:
> When a new_bid comes in, I:
> 1) retrieve the current_high_bid field of the Auction object
> 2) compare it with the new_bid
> 3) if the new bid is higher than current_high_bid, I update
> current_high_bid to the value of the new_bid.
>
> but there's a (slight) chance that between steps (1) and (3) a
> different user could've submitted an even higher bid which then become
> the current_high_bid. In that case step 3 should not proceed. What can
> I do to make sure such a situation does not arise?

This is, of course, exactly what database locking what designed to  
handle (indeed, this is pretty much a textbook case).  Right now,  
Django doesn't provide any built-in method of doing row-level locking  
through the model.  (There's an open ticket that discusses this: 
http://code.djangoproject.com/ticket/2705 
  )

For now, you can write custom SQL (consider this pseudo-code rather  
than a drop-in replacement):

        """ Be sure this all runs inside a single transaction!  Use the  
appropriate middleware or
            decorators... """
        cursor = connection.cursor()
        cursor.execute("SELECT high_bid FROM auctionapp_auction WHERE id=%s  
FOR UPDATE", [auction.id])
            """ Until this SELECT, other users could have modified the high  
bid, so do not trust the
                in-memory copy. """
        returned_rows = cursor.fetchall()
        high_bid = returned_rows[0][0]
        if new_bid > high_bid:
            auction.high_bid = new_bid
            auction.save()

If another user attempts to update the bid, it will block when doing  
the SELECT... FOR UPDATE until the first transaction closes, and will  
then continue.  Beware of deadlocks!  Keep the number of tables on  
which you acquire locks to a minimum, and acquire them in the same  
order in all places in your code.
--
-- Christophe Pettus
    x...@thebuild.com


--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to