[Mpuls-commits] r5547 - base/trunk/mpulsweb/model

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Nov 4 20:28:43 CET 2011


Author: bh
Date: 2011-11-04 20:28:43 +0100 (Fri, 04 Nov 2011)
New Revision: 5547

Modified:
   base/trunk/mpulsweb/model/document.py
Log:
Simplify transaction logic in Document.create.
Use the db.transaction() context manager instead of having the whole
logic for dealing with a db transation inline


Modified: base/trunk/mpulsweb/model/document.py
===================================================================
--- base/trunk/mpulsweb/model/document.py	2011-11-04 19:13:37 UTC (rev 5546)
+++ base/trunk/mpulsweb/model/document.py	2011-11-04 19:28:43 UTC (rev 5547)
@@ -29,6 +29,8 @@
 # Bernhard Herzog <bernhard.herzog at intevation.de>
 #
 
+from __future__ import with_statement
+
 import base64
 from mimetypes import guess_type
 import xml.etree.ElementTree as ET
@@ -183,12 +185,7 @@
         bytes = src 
         self.size = len(bytes) #total_size
 
-        has_commited = False
-        con, cur = None, None
-        try:
-            con = db.getConnection()
-            cur = con.cursor()
-
+        with db.transaction() as cur:
             if case is None:
                 cur.execute(CREATE_GLOBAL, {'name': name})
             else:
@@ -211,25 +208,8 @@
 
             cur.execute(stmnt, fields)
 
-            con.commit()
-            has_commited = True
-
             self.id = id
-        finally:
-            if cur:
-                try:
-                    cur.close()
-                except:
-                    pass
 
-            if not has_commited and con:
-                try:
-                    con.rollback()
-                except:
-                    pass
-
-            db.recycleConnection(con)
-
     def load(self, asCase=False):
         if self.id is None:
             raise DocumentException(_(u"An ID is needed"))



More information about the Mpuls-commits mailing list