I am utterly confused as to how the locking mechanism in sqlalchemy-psql works. I am running a python-flask app with sqlalchemy and postgres. Since i have multiple threads processing some data and updating it on psql, I am getting the following deadlock:
2015-12-31 17:[ERROR] (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely) (psycopg2.extensions.TransactionRollbackError) deadlock detected
DETAIL: Process 22833 waits for ShareLock on transaction 14114188; blocked by process 19759.
Process 19759 waits for ShareLock on transaction 14114189; blocked by process 22833.
Is this how a deadlock may arise:
Thread 1 Thread 2
| (start an sqlalchemy session) |
db.session() db.session()
|(Using sqlalchemy) |
Update row1 of table1 Update row2 of table 1
| |
Update row2 of table1 Update row1 of table1
| |
session.commit() session.commit()
Here are some answers to my problem but i am unable to relate them to sqlalchemy.