Code

Opened 5 years ago

Closed 5 years ago

#11076 closed (duplicate)

@transaction.commit_on_success does not seem to work as documented

Reported by: Pierre Owned by: nobody
Component: Database layer (models, ORM) Version: 1.0
Severity: Keywords: @commit_on_success, set_dirty
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Observed on 1.0.2, but seems to be the same on 1.1.

Note that this is similar to ticket #9964, but not identical (the
issue presented here is much narrower).

I'm working on a simple view using the @transaction.commit_on_success
decorator.

The view does UPDATE requests to a legacy database (through a custom
Python module).

Sample code (simplified)

@transaction.commit_on_success
def myview(request):
  # ...
  if request.method == "POST":
    # no Django model calls;
    # some calls causing an UPDATE
  # ...
  return render_to_response(...)

The work is always rolled-back by Django at the end of the view.

From django.db.transaction it appears that the commit() is done
only if a call to set_dirty() has been made previously:

                if is_dirty():
                    commit()

Which in my case does not happen (unless I call it explicitly, of
course).
The documentation states:

If the function returns successfully, then Django will commit all work done
within the function at that point. If the function raises an exception, though, Django will
roll back the transaction.

As compared with the description of autocommit, which states things
explicitly, this seems to imply that the commit() is done in all
cases, dirty or not, so I found the actual behaviour surprising.

I don't know where is the best place to fix this, code or doc, but
in case you think it's the code... see attached patch :-)

Attachments (1)

transaction.diff (582 bytes) - added by Pierre 5 years ago.
patch for django/db/transaction.py

Download all attachments as: .zip

Change History (3)

Changed 5 years ago by Pierre

patch for django/db/transaction.py

comment:1 Changed 5 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Dupe of #9964

comment:2 Changed 5 years ago by Alex

  • Resolution set to duplicate
  • Status changed from new to closed

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.