2
我正在嘗試SELECT FOR SHARE表中的一組行,以便它們被鎖定直到事務結束。我正在使用SQLAlchemy 0.7.9在PostgreSQL 9.1.6數據庫中執行此操作。這是有問題的Python代碼:如何使用SQLAlchemy和PostgreSQL選擇共享?
NUM_TERMS = 10
conn = engine.connect()
get_terms = select([search_terms.c.term_id, search_terms.c.term],
and_(search_terms.c.lock==False,
search_terms.c.status==False),
order_by=search_terms.c.term,
limit=NUM_TERMS, for_update="read")
trans = conn.begin()
try:
search_terms = conn.execute(get_terms).fetchall()
for term in search_terms:
lock_terms = update(search_terms).\
where(search_terms.c.term_id==term.term_id).\
values(lock=True)
conn.execute(lock_terms)
if trans.commit():
<do things with the search terms>
except:
trans.rollback()
的問題是上面的選擇代碼生成的SQL查詢是不是爲了分享,這是FOR UPDATE:
SELECT search_terms.term_id, search_terms.term
FROM search_terms
WHERE search_terms.lock = :lock_1 AND search_terms.status = :status_1
ORDER BY search_terms.term
LIMIT :param_1 FOR UPDATE
按照SQLAlchemy API docs,下「for_update」參數說明:
使用Postgresql方言,值「read」和「read_nowait」翻譯爲FOR SHARE和FOR NOWAREA,分別爲LY。
根據以上所述,編譯的SQL語句應該是FOR SHARE,但它不是。我的代碼中的錯誤在哪裏?