Commits

Gaël Le Mignot committed 4180ec3

More robust code for subtransaction handling

Comments (0)

Files changed (4)

+sesql (0.14.7-1) unstable; urgency=low
+
+  * More robust code for subtransaction handling
+
+ -- Gael Le Mignot <gael@pilotsystems.net>  Wed, 05 Dec 2012 17:28:42 +0200
+
 sesql (0.14.6-1) unstable; urgency=low
 
   * Fix a transaction issue with Django non-managed transactions

sesql/__init__.py

 # You should have received a copy of the GNU General Public License
 # along with SeSQL.  If not, see <http://www.gnu.org/licenses/>.
 
-VERSION = (0, 14, 6)
+VERSION = (0, 14, 7)
 __version__ = ".".join(map(str, VERSION))
         log.info("%s: no table found, skipping" % message)
         return
 
-    cursor.execute('SAVEPOINT sesql_index_savepoint')
-
     query = "DELETE FROM %s WHERE id=%%s AND classname=%%s" % table_name
     cursor.execute(query, (objid, classname))
 

sesql/orm/django/core.py

         from models import SearchHit
         SearchHit(**kwargs).save()
 
+    def get_txlvl(self):
+        """
+        Get current subtransaction level
+        """
+        return getattr(connection, '_sesql_subtransaction_level', 0)
+
+    def inc_txlvl(self):
+        """
+        Increment the subtransaction level, return the new one
+        """
+        txlvl = self.get_txlvl() + 1
+        connection._sesql_subtransaction_level = txlvl
+        return txlvl
+
+    def dec_txlvl(self):
+        """
+        Decrement the subtransaction level, return the previous one
+        """
+        txlvl = self.get_txlvl()
+        if txlvl:
+            connection._sesql_subtransaction_level = txlvl - 1
+        return txlvl
+
+    def begin(self):
+        """
+        Get a cursor with an open sub-transaction
+        """
+        cursor = self.cursor()
+        txlvl = self.inc_txlvl()
+        cursor.execute('SAVEPOINT sesql_savepoint_%d' % txlvl)
+        return cursor
+
     def commit(self, cursor):
         """
         Commit sub-transaction on cursor
         """
-        super(DjangoOrmAdapter, self).commit(cursor)    
-        transaction.commit_unless_managed()
+        txlvl = self.dec_txlvl()
+        if txlvl:
+            cursor.execute('RELEASE SAVEPOINT sesql_savepoint_%d' % txlvl)
+        if txlvl == 1:
+            # Last subtransaction level ? Commit
+            transaction.commit_unless_managed()            
+
+    def rollback(self, cursor):
+        """
+        Rollback sub-transaction on cursor
+        """
+        txlvl = self.dec_txlvl()
+        if txlvl:
+            cursor.execute('ROLLBACK TO SAVEPOINT sesql_savepoint_%d' % txlvl)
+