Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设计的问题:潜在的数据一致性 bug #13

Open
AllanChain opened this issue Nov 19, 2021 · 0 comments
Open

设计的问题:潜在的数据一致性 bug #13

AllanChain opened this issue Nov 19, 2021 · 0 comments

Comments

@AllanChain
Copy link
Member

众所周知,db.session.commit() 用于提交对数据库的更改。现在的设计中,所有的 commit 都放在数据库操作的专门模块中。虽然这样有助于解耦,但是这个引入了潜在的数据一致性问题。

想象这样一个场景:

@route
def handle():
    result_a = process_a()
    DB.put_something(result_a)
    result_b = process_b()
    DB.put_otherthing(result_b)

实际出现的场景如下 43, 59 行:

Datax10n.put_name(openid, result["name"], result["stuID"])
prob_ids = Datax10n.get_probs(openid)
questions = result["questions"]
if len(questions) != settings.x10n.PROBLEMS_NUMBER:
return {"msg": "you have submit a list with wrong probids"}
for question in questions:
if question["number"] not in prob_ids:
return {"msg": "you have submit a list with wrong probids"}
question["answer"] = str(question["answer"]) == Datax10nProbs.get_ans(
question["number"]
)
user_result = {
"time": time_used,
"name": result["name"],
"questions": questions,
}
Datax10n.put_info(openid, user_result)

如果 process_b 出了问题,那么数据库中的数据会出现意想不到的矛盾状态,这是我们不想看到的。

经典的解决方法就是一起 db.session.commit(),两个写入操作一起提交,要么一起成功,要么都别写入,保证了数据的一致性。

所以需要对这部分的设计作更改。

@zqi-wong zqi-wong pinned this issue Aug 5, 2022
@Kongliu007 Kongliu007 unpinned this issue Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant