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?
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]
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With