Hello guys, I tried what Andy suggested and its not working. Basically had two shells:
In [1]: u = User.get(1) In [2]: u Out[2]: <tgWeb.model.User object at 0xb725f40c> In [3]: u.visit_counter += 1 In [4]: u.flush() I did the above. u.flush() was done at the end in both the shells. Final value of visit_counter is 1 (initial value is 0). I'll check on alberto's suggestion. thanks, -tml On Mar 15, 5:04 am, Alberto Valverde <[EMAIL PROTECTED]> wrote: > On Mar 15, 2007, at 1:08 AM, Andrew Grover wrote: > > > > > > > On 3/14/07, tml <[EMAIL PROTECTED]> wrote: > >> I'm using SA and have a visit counter for user profiles and some > >> other > >> pages. I have a visit_counter column in User object, and usually do > >> User.get(id).visit_counter+=1 > > >> But I realize this is a race if all the threads are serving the same > >> page. Can someone tell how to do this right? > > > If your database implements ACID features (pretty much all?) then it > > will ensure that the race is prevented. > > > You can test this by opening two tg shells -- you will see that if > > they both access the same row, the second one will freeze until the > > first one's transaction ends. > > I don't think that it would turn out this way unless some sort of > "SELECT FOR UPDATE" statement is issued by the orm. The possibility > exists of two threads reading "5", in differerent transactions, and > commiting back "6", effectively counting only one visitor. Or maybe > throwing a non-serializable update error (if using postgres). > > The table should be locked from concurrent updates when the value is > read, something like: > > table.select(table.c.id==id, for_update=True) > > I'm not sure if you can do it with a mapper, better ask in the SA list. > > Alberto --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "TurboGears" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/turbogears?hl=en -~----------~----~----~----~------~----~------~--~---

