很抱歉问这个问题,但在搜索这个问题后我找不到任何有用的答案。
我知道在 SQLAlchemy 的会话中,update()将无法工作,甚至可能无法与数据库通信,直到您使用session.commit.
这是我的代码,我认为我使用的技能称为optimistic lock:
with Session() as session:
# get one record
object = session.query(Table).where(key=param).limit(1)
content = "here is the calculated value of my business"
# update the record by the id of the gotten record
# if 2 thread get the same record and run into this update statement, I think only can success
# because it is ensured by the MySQL MVCC
Table.update(Table).set(Table.content = content).where(id = object.id)
sessoin.commit()
但是我在这里有一个问题,如果我想在in the thread which got the lock之后做点什么session.commit()?
比如我期望的代码是这样的:
affected_rows = sessoin.commit()
if affected_rows:
do_something_after_get_lock_success()
有人可以帮忙吗?非常感谢!^_^
开心每一天1111
相关分类