[SQL-CVS] r1176 - SQLObject/trunk/sqlobject
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: <sub...@co...> - 2005-10-31 16:54:29
|
Author: test
Date: 2005-10-31 16:54:16 +0000 (Mon, 31 Oct 2005)
New Revision: 1176
Modified:
SQLObject/trunk/sqlobject/dbconnection.py
Log:
Added method to ConnectionHub objects to run code in a transaction
Modified: SQLObject/trunk/sqlobject/dbconnection.py
===================================================================
--- SQLObject/trunk/sqlobject/dbconnection.py 2005-10-31 16:01:38 UTC (rev 1175)
+++ SQLObject/trunk/sqlobject/dbconnection.py 2005-10-31 16:54:16 UTC (rev 1176)
@@ -902,6 +902,34 @@
"No connection has been defined for this thread "
"or process")
+ def doInTransaction(self, func, *args, **kw):
+ """
+ This routine can be used to run a function in a transaction,
+ rolling the transaction back if any exception is raised from
+ that function, and committing otherwise. Use like::
+
+ sqlhub.doInTransaction(process_request, os.environ)
+
+ This will run ``process_request(os.environ)``. The return
+ value will be preserved.
+ """
+ # @@: In Python 2.5, something usable with with: should also
+ # be added.
+ old_conn = self.getConnection()
+ conn = old_conn.transaction()
+ self.threadConnection = conn
+ try:
+ try:
+ value = func(*args, **kw)
+ except:
+ conn.rollback()
+ raise
+ else:
+ conn.commit()
+ return value
+ finally:
+ self.threadConnection = old_conn
+
def _set_threadConnection(self, value):
self.threadingLocal.connection = value
|