Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manual commit in Django 1.8

How do you implement @commit_manually in Django 1.8?

I'm trying to upgrade Django 1.5 code to work with Django 1.8, and for some bizarre reason, the commit_manually decorator was removed in Django 1.6 with no direct replacement. My process iterates over thousands of records, so it can't wrap the entire process in a single transaction without running out of memory, but it still needs to group some records in a transaction to improve performance. To do this, I had a method wrapped with @commit_manually, which called transaction.commit() every N iterations.

I can't tell for sure from the docs, but this still seems supported. I just have to call set_autocommit(False) instead of having a convenient decorator. Is this correct?

like image 470
Cerin Avatar asked Apr 23 '15 18:04

Cerin


People also ask

How do I commit a transaction in Django?

Django provides the on_commit() function to register callback functions that should be executed after a transaction is successfully committed: on_commit (func, using=None)[source]

How does Django handle concurrency?

In terms of Django, optimistic concurrency control can be implemented by overriding the save method on your model class... And, of course, for either of these concurrency mechanisms to be robust, you have to consider transactional control.


1 Answers

Yeah, you've got it. Call set_autocommit(False) to start a transaction, then call commit() and set_autocommit(True) to commit it.

You could wrap this up in your own decorator:

def commit_manually(fn):
    def _commit_manually(*args, **kwargs):
        set_autocommit(False)
        res = fn(*args, **kwargs)
        commit()
        set_autocommit(True)
        return res
    return _commit_manually
like image 96
mipadi Avatar answered Sep 30 '22 08:09

mipadi