Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple processes accessing Django db backend; records not showing up until manually calling _commit

Tags:

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

like image 462
Asif Rahman Avatar asked Jul 10 '09 00:07

Asif Rahman


1 Answers

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.

like image 192
Vinay Sajip Avatar answered Oct 12 '22 08:10

Vinay Sajip