#27504: Cannot Make ORM Queries After an Error and Rollback In Non-autocommit
Mode
-------------------------------------+-------------------------------------
Reporter: Mark Young | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):
* type: Uncategorized => Bug
* stage: Unreviewed => Ready for checkin
Old description:
> In non-autocommit mode, after an error has occurred (specifcally,
> IntegrityError and ValidationErrors are known to cause the issue, but I
> haven't fully investigated which errors can cause this), the db
> connection's needs_rollback attribute is set to True. When the flag is
> set to True, no further orm queries can be made to the database. Once
> this has happened, there appears to be no way to clean up the
> transaction, as a transaction.rollback() does not unset this flag.
>
> An example failing scenario:
>
> {{{
> transaction.set_autocommit(False)
> r1 = Reporter.objects.create(first_name="Archibald",
> last_name="Haddock")
> r2 = Reporter(first_name="Cuthbert", last_name="Calculus", id=r1.id)
> try:
> r2.save(force_insert=True)
> except IntegrityError:
> transaction.rollback()
> # This raises a TransactionManagementError
> last_reporter = Reporter.objects.last()
> }}}
>
> This ticket is related to https://code.djangoproject.com/ticket/26340 and
> solves a subset of the scenarios affected by it.
New description:
In non-autocommit mode, after an error has occurred (specifcally,
`IntegrityError` and `ValidationError` are known to cause the issue, but I
haven't fully investigated which errors can cause this), the db
connection's `needs_rollback` attribute is set to True. When the flag is
set to `True`, no further ORM queries can be made to the database. Once
this has happened, there appears to be no way to clean up the transaction,
as a `transaction.rollback()` does not unset this flag.
An example failing scenario:
{{{
transaction.set_autocommit(False)
r1 = Reporter.objects.create(first_name="Archibald",
last_name="Haddock")
r2 = Reporter(first_name="Cuthbert", last_name="Calculus", id=r1.id)
try:
r2.save(force_insert=True)
except IntegrityError:
transaction.rollback()
# This raises a TransactionManagementError
last_reporter = Reporter.objects.last()
}}}
This ticket is related to #26340 and solves a subset of the scenarios
affected by it.
--
Comment:
[https://github.com/django/django/pull/7577 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/27504#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.24742a288aac9b99be36cd2fd39ef823%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.