Index: db/transaction.py
===================================================================
--- db/transaction.py	(revision 9832)
+++ db/transaction.py	(working copy)
@@ -12,6 +12,7 @@
 or implicit commits or rollbacks.
 """
 
+import contextlib
 try:
     import thread
 except ImportError:
@@ -265,3 +266,29 @@
             leave_transaction_management()
 
     return wraps(func)(_commit_manually)
+
+####################
+# CONTEXT MANAGERS #
+####################
+@contextlib.contextmanager
+def transaction():
+    """Context manager for executing something in the context of a database
+       transaction. A transaction is started and the nested block executed. If
+       no exceptions are raised, the transaction is committed. Otherwise, the
+       transaction is rolled back and the exception re-raised."""
+    try:
+        enter_transaction_management()
+        managed(True)
+        try:
+            yield
+        except:
+            # The nested block threw an exception, roll back
+            if is_dirty():
+                rollback()
+            raise
+        else:
+            # The nested block succeeded, commit
+            if is_dirty():
+                commit()
+    finally:
+        leave_transaction_management()
