多进程访问Django数据库后端;手动调用_commit之前,不会显示记录 [英] Multiple processes accessing Django db backend; records not showing up until manually calling _commit
问题描述
这个问题以前被问过:
MySQL响应中的缓存问题与Django中的MySQLdb
OP表示他解决了这个问题,但并没有完全解释如何。任何人都可以看出这一点吗?我想要能够访问记录,而无需手动调用_commit()。
谢谢,
Asif
他说:
Django的自动提交不是db中的实际自动提交。
所以,你必须确保自动提交设置在数据库级别。否则,由于事务隔离,进程将不会看到由不同的进程(不同的连接)所做的更改,直到提交完成。 AFAIK这不是特别的Django问题,除了关于Django autocommit的文档不够清晰!= db autocommit。
更新:从MySQL文档中稍作改写:
REPEATABLE READ是InnoDB的默认
隔离级别。对于
一致的读取,有一个
与READ
COMMITTED隔离级别的重要区别:所有
同一个
事务中的一致读取读取快照
由第一个读取建立。 (我的
重点。)
所以,使用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:
Django's autocommit isn't an actual autocommit in the db.
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:
REPEATABLE READ is the default isolation level for InnoDB. For consistent reads, there is an important difference from the READ COMMITTED isolation level: All consistent reads within the same transaction read the snapshot established by the first read. (My emphasis.)
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之前,不会显示记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!