Opened 6 years ago

Closed 4 years ago

Last modified 3 years ago

#10771 closed (fixed)

Add a transaction context manager

Reported by: obeattie Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: transaction, contextmanager
Cc: chromano Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

For users who use transactions heavily, it is sometimes desirable to be able to wrap parts of a function (but not all of it) in a transaction of its own. Previously, the best way to do this was either to do it manually with a try…except…else block or creating a sub-function wrapped in commit_on_success, but context managers in Python 2.5+ can make this much cleaner by just saying…

with transaction:
    # do stuff

…which will handle committing and rolling back. Attached is a patch that I'd love to see implemented in Django :) The function is extremely similar to commit_on_success (the structure is identical).

Attachments (5)

contextmanager.patch (1.2 KB) - added by obeattie 6 years ago.
Adds a transaction context manager
contextmanager-v2.patch (965 bytes) - added by obeattie 6 years ago.
Second patch with support for <Python 2.5
contextmanager-v3.patch (1007 bytes) - added by obeattie 6 years ago.
Python 2.3 compatibility
django-transactions.diff (8.3 KB) - added by Alex 5 years ago.
django-transactions.2.diff (13.4 KB) - added by Alex 4 years ago.

Download all attachments as: .zip

Change History (15)

Changed 6 years ago by obeattie

Adds a transaction context manager

comment:1 Changed 6 years ago by Alex

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 6 years ago by ericholscher

Seems like something that couldn't be supported until 1.3 at least (or whenever we drop 2.4 support).

comment:3 Changed 6 years ago by lukeplant

Actually, defining a context manager does not need the 'with' statement, so it is perfectly possible to add this, providing you only define it is 'contextlib' can be imported (which isn't true for Python < 2.5). Of course, only people running Python 2.5 or later would be able to make use of this. There is the problem of writing tests, but these could either be written explicitly using the magic methods __enter__, __exit__ etc., or they could be omitted since this stuff is pretty well covered by the tests for commit_on_success.

comment:4 Changed 6 years ago by obeattie

This can be pretty easily rewritten as you say with __enter__ and __exit__, thereby circumventing the need for contextlib. I'll update the patch.

Changed 6 years ago by obeattie

Second patch with support for <Python 2.5

comment:5 Changed 6 years ago by lukeplant

I was actually referring to writing the tests by calling __enter__() etc. explicitly, but your change is probably a good idea anyway. It still is not compatible with Python 2.3, due to decorator syntax, which is trivial to fix.

comment:6 Changed 6 years ago by obeattie

Oh I see. I don't think it would that big of a deal in that regard — it would require some fiddling around when passing errors to __exit__, but it's really a simple case of doing __exit__(*sys.exc_info()), so not that kludgy.

Changed 6 years ago by obeattie

Python 2.3 compatibility

Changed 5 years ago by Alex

comment:7 Changed 5 years ago by Alex

  • milestone set to 1.3
  • Triage Stage changed from Design decision needed to Accepted
  • Version changed from 1.0 to SVN

comment:8 Changed 4 years ago by anonymous

  • Cc chromano added

Changed 4 years ago by Alex

comment:9 Changed 4 years ago by Alex

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

(In [14288]) Fixed #10771 -- added support for using the transaction management functions as context managers in Python 2.5 and above. Thanks to Jacob for help with the docs.

comment:10 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

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