Opened 7 years ago

Closed 7 years ago

#27504 closed Bug (fixed)

Cannot Make ORM Queries After an Error and Rollback In Non-autocommit Mode

Reported by: Mark Young Owned by: nobody
Component: Database layer (models, ORM) Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

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.

Change History (2)

comment:1 by Tim Graham, 7 years ago

Description: modified (diff)
Triage Stage: UnreviewedReady for checkin
Type: UncategorizedBug

comment:2 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: newclosed

In 2742901:

Fixed #27504 -- Allowed using the ORM after an error and rollback when autocommit is off.

Note: See TracTickets for help on using tickets.
Back to Top