Xanathos
Xanathos

Reputation: 598

How to lock tables with SQL Alchemy (PostgreSQL)?

I'm using session with a room reservation app that uses SQL Alchemy. The app until now works fine, but, I tested with 2 persons using it at the same time, using different user accounts, and the reserves can be done at the same room, and hour of the day.

This didn't happen when using just one account, i tested the room check code a lot, so must be a concurrency problem (both users use the check when is empty and the make it). I'm using PostgreSQL as DBMS, is there a way lock the table while making the commit to the DB and then unlock it when it's done??

Upvotes: 3

Views: 1803

Answers (1)

kgrittn
kgrittn

Reputation: 19471

This is exactly the kind of case for which exclusion constraints where invented. When 9.2 is released you will be able to use these with timestamptz ranges; for versions 9.0 and 9.1 you will need to install the the temporal and btree_gist modules.

For more information see this blog post by the Jeff Davis, who came up with the idea and implemented it.

Upvotes: 2

Related Questions