On Sep 11, 2007, at 2:25 AM, Hermann Himmelbauer wrote:

>
> Hi,
> I need to safely increase a numeric value via SQLAlchemy:
> I have a table that has a column with numeric data. For increasing  
> the value,
> I need to read the data, add some value and store it, so it would  
> look like
> that:
>
> - select myvalue from mytable
> - myvalue += 123
> - update mytable and set myvalue to 123
>
> However, problems arise when two of this operations are done  
> simultaneously,
> e.g. operation A and B:
>
> A - select
> B - select
> A - increase
> B - increase
> A - update
> B - update
>
> In this case, operation A is overwritten by B.
>
> One viable solution would be to make the operation atomic, by e.g.  
> locking the
> database row. Is this possible with SQLAlchemy? If yes, how?
>
> Or is there a better way?
>


we can lock rows using SELECT ... FOR UPDATE expressions.   the select 
() construct includes a "for_update=True" flag for this, and the  
ORM's Query object supports a with_lockmode() method that can add FOR  
UPDATE semantics to the SELECT statement.  the docstrings for select 
() and Query should explain both.



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

Reply via email to