Tuesday, July 28, 2015

Re: Database inserts with autocommit set to off.

Thanks a lot! I've ended up using `django-transaction-hooks`.

On Monday, July 27, 2015 at 5:49:40 PM UTC+2, Carl Meyer wrote:
Hi Maciej,

On 07/27/2015 07:03 AM, Maciej Gol wrote:
> I've been recently working (porting to Django 1.8) on a project where we
> need to use a few transactions in a single request, and these
> transactions cannot be correctly handled by the `atomic` decorator due
> to functions calls nesting. Basically, we are sending celery tasks at
> the end of some processing, and it requires the results to be visible in
> the database. I'm doing this for each element of a list, thus the celery
> task sending is done right after saving the data to the database. The
> commit should happen between the save and posting the task, and since
> the processing logic is complex, I can't use a decorator here.

I think a better solution to this situation is to use
transaction.atomic, and then use django-transaction-hooks [1] to delay
creation of the Celery task(s) until the transaction is successfully
committed. (Django 1.9 will have transaction-hooks integrated into core.)

 [1] http://django-transaction-hooks.readthedocs.org/en/latest/

> The issue is, when autocommit is set to off, whenever I try to
> `.update()` a `QuerySet` or `.save()` a `Model`, it results in
> `TransactionManagementError: The outermost 'atomic' block cannot use
> savepoint = False when autocommit is off.` error, which is kind of sad
> because I could handle the eventual rollback myself gracefully. Instead,
> django throws me this error in the face.

Yes, this is a known issue in 1.8:
https://code.djangoproject.com/ticket/24921

The ticket describes the needed solution in some detail, it just remains
for someone to code up the patch with a test. Since the issue is a
regression in 1.8, I think such a patch would be backported to the 1.8
branch and appear in the next 1.8.x release.

Carl

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ed7073ba-51e9-46b3-8594-aa5fb0c35a39%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment