[session] provide a simple transaction context manager for session. Closes #1725640 stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 06 Jun 2011 14:41:00 +0200
branchstable
changeset 7451 48ba5f0c11de
parent 7450 c21d845836e4
child 7452 7e8182a63559
[session] provide a simple transaction context manager for session. Closes #1725640
server/session.py
--- a/server/session.py	Wed Jun 01 10:20:58 2011 +0200
+++ b/server/session.py	Mon Jun 06 14:41:00 2011 +0200
@@ -75,6 +75,25 @@
     return req.is_internal_session
 
 
+class transaction(object):
+    """context manager to enter a transaction for a session: when exiting the
+    `with` block on exception, call `session.rollback()`, else call
+    `session.commit()` on normal exit
+    """
+    def __init__(self, session, free_cnxset=True):
+        self.session = session
+        self.free_cnxset = free_cnxset
+
+    def __enter__(self):
+        pass
+
+    def __exit__(self, exctype, exc, traceback):
+        if exctype:
+            self.session.rollback(free_cnxset=self.free_cnxset)
+        else:
+            self.session.commit(free_cnxset=self.free_cnxset)
+
+
 class hooks_control(object):
     """context manager to control activated hooks categories.
 
@@ -188,6 +207,16 @@
         return '<%ssession %s (%s 0x%x)>' % (
             self.cnxtype, unicode(self.user.login), self.id, id(self))
 
+    def transaction(self, free_cnxset=True):
+        """return context manager to enter a transaction for the session: when
+        exiting the `with` block on exception, call `session.rollback()`, else
+        call `session.commit()` on normal exit.
+
+        The `free_cnxset` will be given to rollback/commit methods to indicate
+        wether the connections set should be freed or not.
+        """
+        return transaction(self, free_cnxset)
+
     def set_tx_data(self, txid=None):
         if txid is None:
             txid = threading.currentThread().getName()