问题描述
我有一个Django项目,其中多个进程正在访问后端的mysql数据库。一个过程是创建记录,而第二个过程正在尝试读取这些记录。我遇到一个问题,在我手动调用connection._commit()之前,尝试读取记录的第二个进程实际上找不到记录。这个问题以前被问过:
OP表示他解决了这个问题,但并没有完全解释如何。任何人都可以看出这一点吗?我想要能够访问记录,而无需手动调用_commit()。
谢谢,
Asif
他说:
所以,你必须确保自动提交设置在数据库级别。否则,由于事务隔离,进程将不会看到由不同的进程(不同的连接)所做的更改,直到提交完成。 AFAIK这不是特别的Django问题,除了关于Django autocommit的文档不够清晰!= db autocommit。
更新:从MySQL文档中稍作改写:
所以,使用REPEATABLE READ,只有在随后的阅读中,先读使用READ COMMITTED,每个读取创建并读取其自己的新鲜快照,以便您看到其他事务的后续更新。所以 - 在回答您的评论时,您对交易水平的变更是正确的。
I have a Django project in which multiple processes are accessing the backend mysql db. One process is creating records, while a second process is trying to read those records. I am having an issue where the second process that is trying to read the records can't actually find the records until I manually call connection._commit().
This question has been asked before:caching issues in MySQL response with MySQLdb in Django
The OP stated that he solved the problem, but didn't quite explain how. Can anyone shed some light on this? I'd like to be able to access the records without manually calling _commit().
Thanks,
Asif
He said:
So, you have to ensure that autocommit is set at the db level. Otherwise, because of transaction isolation, processes will not see changes made by a different process (different connection), until a commit is done. AFAIK this is not especially a Django issue, other than the lack of clarity in the docs about Django autocommit != db autocommit.
Update: Paraphrasing slightly from the MySQL docs:
So, with REPEATABLE READ you only get, on subsequent reads, what was read in the first read. With READ COMMITTED, each read creates and reads its own fresh snapshot, so that you see subsequent updates from other transactions. So - in answer to your comment - your change to the transaction level is correct.
这篇关于多进程访问Django数据库后端;手动调用_commit之前,不会显示记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!