Opened 7 years ago

Closed 6 years ago

Last modified 5 years ago

#10771 closed (fixed)

Add a transaction context manager

Reported by: Oliver Beattie Owned by: nobody
Component: Database layer (models, ORM) Version: master
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: 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 Oliver Beattie 7 years ago.
Adds a transaction context manager
contextmanager-v2.patch (965 bytes) - added by Oliver Beattie 7 years ago.
Second patch with support for <Python 2.5
contextmanager-v3.patch (1007 bytes) - added by Oliver Beattie 7 years ago.
Python 2.3 compatibility
django-transactions.diff (8.3 KB) - added by Alex Gaynor 6 years ago.
django-transactions.2.diff (13.4 KB) - added by Alex Gaynor 6 years ago.

Download all attachments as: .zip

Change History (15)

Changed 7 years ago by Oliver Beattie

Attachment: contextmanager.patch added

Adds a transaction context manager

comment:1 Changed 7 years ago by Alex Gaynor

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedDesign decision needed

comment:2 Changed 7 years ago by Eric Holscher

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

comment:3 Changed 7 years ago by Luke Plant

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

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

Attachment: contextmanager-v2.patch added

Second patch with support for <Python 2.5

comment:5 Changed 7 years ago by Luke Plant

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

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

Attachment: contextmanager-v3.patch added

Python 2.3 compatibility

Changed 6 years ago by Alex Gaynor

Attachment: django-transactions.diff added

comment:7 Changed 6 years ago by Alex Gaynor

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

comment:8 Changed 6 years ago by anonymous

Cc: Henrique Romano added

Changed 6 years ago by Alex Gaynor

Attachment: django-transactions.2.diff added

comment:9 Changed 6 years ago by Alex Gaynor

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 Changed 5 years ago by Jacob

milestone: 1.3

Milestone 1.3 deleted

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