[serverctl] add db_transaction context manager
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Fri, 19 Dec 2014 12:08:01 +0100
changeset 10124 153c969b3a05
parent 10123 c390c444de06
child 10125 bc6461a7d2da
[serverctl] add db_transaction context manager
server/serverctl.py
--- a/server/serverctl.py	Thu Mar 28 19:24:46 2013 +0100
+++ b/server/serverctl.py	Fri Dec 19 12:08:01 2014 +0100
@@ -207,6 +207,22 @@
 
 
 @contextmanager
+def db_transaction(source, privilege):
+    """Open a transaction to the instance database"""
+    cnx = system_source_cnx(source, special_privs=privilege)
+    cursor = cnx.cursor()
+    try:
+        yield cursor
+    except:
+        cnx.rollback()
+        cnx.close()
+        raise
+    else:
+        cnx.commit()
+        cnx.close()
+
+
+@contextmanager
 def db_sys_transaction(source, privilege):
     """Open a transaction to the system database"""
     cnx = _db_sys_cnx(source, privilege)