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
-~----------~----~----~----~------~----~------~--~---

Reply via email to