python - SQLAlchemy(Postgresql) - Race Conditions -
we writing inventory system , have questions sqlalchemy (postgresql) , transactions/sessions. web app using tg2, not sure matters info never bad.
how can make sure when changing inventory qty's don't run race conditions. if understand correctly if user on going decrement inventory on item 0 , user 2 trying decrement inventory 0 if user 1s session hasn't been committed yet user 2 starting inventory number going same user 1 resulting in race condition when both commit, 1 overwriting other instead of having compound effect.
if wanted use postgresql sequence things order/invoice numbers how can get/set next values sqlalchemy without running race conditions?
edit: think found solution need use with_lockmode, using update or share. going leave open more answers or others correct me if mistaken.
tia
if 2 transactions try set same value @ same time 1 of them fail. 1 loses need error handling. particular example want query number of parts , update number of parts in same transaction.
there no race condition on sequence numbers. save record uses sequence number db automatically assign it.
edit:
note limscoder points out need set isolation level repeatable read.
Comments
Post a Comment