Opened 16 years ago

Closed 14 years ago

Last modified 13 years ago

#10771 closed (fixed)

Add a transaction context manager

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

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 Oliver Beattie 16 years ago.
Adds a transaction context manager
contextmanager-v2.patch (965 bytes ) - added by Oliver Beattie 16 years ago.
Second patch with support for <Python 2.5
contextmanager-v3.patch (1007 bytes ) - added by Oliver Beattie 16 years ago.
Python 2.3 compatibility
django-transactions.diff (8.3 KB ) - added by Alex Gaynor 15 years ago.
django-transactions.2.diff (13.4 KB ) - added by Alex Gaynor 14 years ago.

Download all attachments as: .zip

Change History (15)

by Oliver Beattie, 16 years ago

Attachment: contextmanager.patch added

Adds a transaction context manager

comment:1 by Alex Gaynor, 16 years ago

Triage Stage: UnreviewedDesign decision needed

comment:2 by Eric Holscher, 16 years ago

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

comment:3 by Luke Plant, 16 years ago

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 by Oliver Beattie, 16 years ago

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

by Oliver Beattie, 16 years ago

Attachment: contextmanager-v2.patch added

Second patch with support for <Python 2.5

comment:5 by Luke Plant, 16 years ago

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 by Oliver Beattie, 16 years ago

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.

by Oliver Beattie, 16 years ago

Attachment: contextmanager-v3.patch added

Python 2.3 compatibility

by Alex Gaynor, 15 years ago

Attachment: django-transactions.diff added

comment:7 by Alex Gaynor, 15 years ago

milestone: 1.3
Triage Stage: Design decision neededAccepted
Version: 1.0SVN

comment:8 by anonymous, 14 years ago

Cc: Henrique Romano added

by Alex Gaynor, 14 years ago

Attachment: django-transactions.2.diff added

comment:9 by Alex Gaynor, 14 years ago

Resolution: fixed
Status: newclosed

(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 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

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