Monday, July 27, 2015

Re: Database inserts with autocommit set to off.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBCAAGBQJVtlLlAAoJEC0ft5FqUuEh0YYQAKwxQ+jyYcadekdy7DFvSmg6
68+4vrKc2IjIZlwkn0sA3qP4xl0LpnZ3oq5bDkJByoIr/H8tC8pDRzo3pG1Dj6J5
bGg0kad565MdriX6cXE1zXcsmNqno4nAcXvpHwRTNbfEyJt13V9HoZb+YhsZooJg
lDBUVnSVuYX/yroGP7awg3lRqJ7dOWpGym77sKGiAsozIRkb5ksp8IQNwi5upJjV
0Kh7xqareuOAsRjfaQi/P46Clki3tcmLj3LXW6n+RGH0oY6AKFknGypjaRuxd17b
lwMT7lzf8fCLZY2qZMsutBJgXVDZiW7c0IV/mcRx2reAirOc8PVi1wRKy/mAguMP
Wp1b8mfxCD1abe/DIKH7xKpGHDC1vW9F6cuhJjdtOOSLfOtL7mpZfALjcFym5oa+
00tMHARni4Uw1pIT5ADSrENKQ4hEAVFaEQF6GP/TxnbV7IqnJn8pbL/j8mxr4Uln
MTpUvrdtOeYm1AywWPX/h8otAgsyhe1oQOg099Y0PKVmLar5zcykg6MRdY40dl76
8QgKdXnzOFjEoUTjX/mI358ewU6l3CfORQ5LSd+bLi7rZF42eoVVTfE9v05iyLOQ
MQHUWsCVHen5qP/KEiL+QW2SpxSD/zm6JlBNwboSTVp4iDSLyAHEV1El+82OMTiu
mhIAp3XII8ssx8kSXfHM
=nunT
-----END PGP SIGNATURE-----
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/55B652E4.2040904%40oddbird.net.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment