[Mpuls-commits] r3121 - in base/trunk: . mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Jul 2 19:58:09 CEST 2010


Author: bh
Date: 2010-07-02 19:58:07 +0200 (Fri, 02 Jul 2010)
New Revision: 3121

Modified:
   base/trunk/ChangeLog
   base/trunk/mpulsweb/lib/db.py
Log:
* mpulsweb/lib/db.py (DB.execute): New.  Handle the common case
where one SQL statement has to be executed as a transaction.  This
should be used in all places where appropriate to make sure that
commit and rollback are handled properly.


Modified: base/trunk/ChangeLog
===================================================================
--- base/trunk/ChangeLog	2010-07-02 16:31:01 UTC (rev 3120)
+++ base/trunk/ChangeLog	2010-07-02 17:58:07 UTC (rev 3121)
@@ -1,5 +1,12 @@
 2010-07-02  Bernhard Herzog  <bh at intevation.de>
 
+	* mpulsweb/lib/db.py (DB.execute): New.  Handle the common case
+	where one SQL statement has to be executed as a transaction.  This
+	should be used in all places where appropriate to make sure that
+	commit and rollback are handled properly.
+
+2010-07-02  Bernhard Herzog  <bh at intevation.de>
+
 	* mpulsweb/model/case.py (MpulsCaseState.setState): Use
 	log.exception instead of printing a traceback directly to stderr.
 

Modified: base/trunk/mpulsweb/lib/db.py
===================================================================
--- base/trunk/mpulsweb/lib/db.py	2010-07-02 16:31:01 UTC (rev 3120)
+++ base/trunk/mpulsweb/lib/db.py	2010-07-02 17:58:07 UTC (rev 3121)
@@ -105,7 +105,34 @@
             except:
                 log.exception("Error ignored while closing connection")
 
+    def execute(self, statement, *args):
+        """Execute an SQL statement in a transaction.
 
+        This method is intended for the common case that a single SQL
+        statement has to be executed in a transaction.  This method gets
+        a connection with getConnection(), creates a cursor object and
+        calls its execute method with the SQL statement and any further
+        arguments as arguments.  If the statement is executed
+        successfully, the transaction is committed.  If an error occurs
+        the transaction is rolled back.  This method does not catch any
+        exceptions.
+
+        Any results retrieved from the database are discarded.
+        """
+        has_commited = False
+        cur = None
+        con = self.getConnection()
+        try:
+            cur = con.cursor()
+            cur.execute(statement, *args)
+            con.commit()
+            has_commited = True
+        finally:
+            if not has_commited and con is not None:
+                con.rollback()
+            db.recycleConnection(con, cur)
+
+
 class DBInterface:
 
     def acquireConnection(self):



More information about the Mpuls-commits mailing list