Commits

Michael Tharp committed 36c5678

Make better use of implicit transactions in the database layer

This fixes "WARNING: there is already a transaction in progress" every
time getVersion is called on postgres, which is every request.

Comments (0)

Files changed (4)

conary/dbstore/base_drv.py

 
 
 import sys
-import traceback
 import re
 
 import sqlerrors, sqllib
 
 DEFAULT_ENCODING = 'UTF-8'
 
-(DEBUG_OFF,
-        DEBUG_WARN, # Print stack on invalid txn ops
-        DEBUG_WARN_ALL, # Print stack on all txn ops
-        DEBUG_ERROR, # Enter debugger on invalid txn ops
-    ) = range(4)
-DEBUG_TRANSACTIONS = DEBUG_OFF
-
 
 # class for encapsulating binary strings for dumb drivers
 class BaseBinary:
         pass
 
     # transaction support
-    def _logTransaction(self, command):
-        """
-        Check for txn sanity, when configured to do so.
-        """
-        # pylint: disable-msg=W0212
-        #  * _getframe needed to make a nicer stack dump.
-        assert command in ('BEGIN', 'ROLLBACK', 'COMMIT')
-        if DEBUG_TRANSACTIONS == DEBUG_OFF:
-            return
-        shouldBeInTrans = (command != 'BEGIN')
-        inTrans = self.inTransaction(default=shouldBeInTrans)
-        if inTrans == shouldBeInTrans:
-            if DEBUG_TRANSACTIONS == DEBUG_WARN_ALL:
-                print >> sys.stderr, 'Transaction: %s at' % (command,)
-                traceback.print_stack(sys._getframe(2))
-            return
-
-        where = (inTrans and 'inside' or 'outside')
-        if DEBUG_TRANSACTIONS == DEBUG_ERROR:
-            print >> sys.stderr, 'Transaction bug: %s %s of transaction' % (command, where)
-            from conary.lib import debugger
-            debugger.st()
-        else:
-            print >> sys.stderr, 'Transaction bug: %s %s of transaction at:' % (command, where)
-            traceback.print_stack(sys._getframe(2))
-
     def commit(self):
         assert(self.dbh)
-        self._logTransaction('COMMIT')
         return self.dbh.commit()
 
     def transaction(self, name = None):
         # basic class does not support savepoints
         assert(not name)
         assert(self.dbh)
-        self._logTransaction('BEGIN')
         c = self.cursor()
         c.execute(self.basic_transaction)
         return c
         # basic class does not support savepoints
         assert(not name)
         assert(self.dbh)
-        self._logTransaction('ROLLBACK')
         return self.dbh.rollback()
 
     @staticmethod

conary/dbstore/sqlite_drv.py

     # sqlite is more peculiar when it comes to firing off transactions
     def transaction(self, name = None):
         assert(self.dbh)
-        self._logTransaction('BEGIN')
         cu = self.cursor()
         if self.dbh.inTransaction:
             return cu

conary/local/schema.py

     except Exception, e:
         return False
 
-def createVersions(db, cu = None):
+def _createVersions(db, cu = None):
     if "Versions" in db.tables:
         return
     if cu is None:
         db.loadSchema()
 
 # Schema creation functions
-def createFlavors(db):
+def _createFlavors(db):
     if "Flavors" in db.tables:
         return
     cu = db.cursor()
     if "DBTroveFiles" in db.tables:
         return
     cu = db.cursor()
-    createVersions(db, cu)
+    _createVersions(db, cu)
     cu.execute("""
     CREATE TABLE DBTroveFiles(
         streamId            %(PRIMARYKEY)s,
     if "Instances" in db.tables:
         return
     cu = db.cursor()
-    createVersions(db, cu)
+    _createVersions(db, cu)
     cu.execute("""
     CREATE TABLE Instances(
         instanceId      %(PRIMARYKEY)s,
     db.commit()
     db.loadSchema()
 
-def createTroveTroves(db):
+def _createTroveTroves(db):
     if "TroveTroves" in db.tables:
         return
     cu = db.cursor()
 def createMetadata(db):
     commit = False
     cu = db.cursor()
-    createVersions(db, cu)
+    _createVersions(db, cu)
     if 'Metadata' not in db.tables:
         cu.execute("""
         CREATE TABLE Metadata(
     db.commit()
     db.loadSchema()
 
-def createDepTable(db, cu, name, isTemp, skipCommit=False):
+def _createDepTable(db, cu, name, isTemp):
     d =  {"tmp" : "", "name" : name}
     startTrans = not isTemp
     if isTemp:
                (name, name), start_transaction = startTrans)
     if isTemp:
         db.tempTables[name] = True
-    if not skipCommit:
-        db.commit()
-    return True
 
-def createRequiresTable(db, cu, name, isTemp, skipCommit=False):
+def _createRequiresTable(db, cu, name, isTemp):
     d = { "tmp" : "",
           "name" : name,
           "constraint" : "",
                start_transaction = startTrans)
     if isTemp:
         db.tempTables[name] = True
-    if not skipCommit:
-        db.commit()
     return True
 
-def createProvidesTable(db, cu, name, isTemp, skipCommit=False):
+
+def _createProvidesTable(db, cu, name, isTemp):
     d = { "tmp" : "",
           "name" : name,
           "constraint" : "" }
     if isTemp:
         db.tempTables[name] = True
 
-    if not skipCommit:
-        db.commit()
-    return True
 
-def createDepWorkTable(db, cu, name, skipCommit=False):
+def _createDepWorkTable(db, cu, name):
     if name in db.tempTables:
         return False
     cu.execute("""
     CREATE INDEX %sIdx ON %s(troveId, class, name, flag)
     """ % (name, name), start_transaction = False)
     db.tempTables[name] = True
-    if not skipCommit:
-        db.commit()
-    return True
 
 # This should be called only once per establishing a db connection
-def setupTempDepTables(db, cu=None, skipCommit=False):
+def setupTempDepTables(db, cu=None):
     if cu is None:
-        db.transaction()
         cu = db.cursor()
-    else:
-        skipCommit = True
-    createRequiresTable(db, cu, "TmpRequires", isTemp=True, skipCommit=True)
-    createProvidesTable(db, cu, "TmpProvides", isTemp=True, skipCommit=True)
-    createDepTable(db, cu, 'TmpDependencies', isTemp=True, skipCommit=True)
-    createDepWorkTable(db, cu, "DepCheck", skipCommit=True)
+    _createRequiresTable(db, cu, "TmpRequires", isTemp=True)
+    _createProvidesTable(db, cu, "TmpProvides", isTemp=True)
+    _createDepTable(db, cu, 'TmpDependencies', isTemp=True)
+    _createDepWorkTable(db, cu, "DepCheck")
 
     if "suspectDepsOrig" not in db.tempTables:
         cu.execute("CREATE TEMPORARY TABLE suspectDepsOrig(depId integer)",
                 nodeId      INTEGER
             )""" % db.keywords, start_transaction = False)
         db.tempTables["RemovedTroves"] = True
+    db.commit()
 
-    if not skipCommit:
-        db.commit()
 
 def createDependencies(db, skipCommit=False):
     commit = False
     cu = db.cursor()
 
     if "Dependencies" not in db.tables:
-        createDepTable(db, cu, "Dependencies", False, skipCommit=True)
+        _createDepTable(db, cu, "Dependencies", isTemp=False)
         commit = True
     if "Requires" not in db.tables:
-        createRequiresTable(db, cu, "Requires", False, skipCommit=True)
+        _createRequiresTable(db, cu, "Requires", isTemp=False)
         commit = True
     if "Provides" not in db.tables:
-        createProvidesTable(db, cu, "Provides", False, skipCommit=True)
+        _createProvidesTable(db, cu, "Provides", isTemp=False)
         commit = True
     if commit:
         if not skipCommit:
         db.commit()
 
 def createSchema(db):
-    createVersions(db)
+    _createVersions(db)
     createInstances(db)
-    createTroveTroves(db)
+    _createTroveTroves(db)
     createDBTroveFiles(db)
-    createFlavors(db)
+    _createFlavors(db)
     createDependencies(db)
     createTroveInfo(db)
     createDataStore(db)
                            SELECT instanceId, troveName, versionId, flavorId,
                                   timeStamps, isPresent, 0 FROM DBInstances
                         """)
-        createFlavors(self.db)
+        _createFlavors(self.db)
         self.cu.execute('INSERT INTO Flavors SELECT * FROM DBFlavors '
                         'WHERE flavor IS NOT NULL')
         self.cu.execute('DROP TABLE DBFlavors')

conary/server/schema.py

 
 # create the (permanent) server repository schema
 def createSchema(db, commit=True):
-    if commit:
-        db.transaction()
-
     if not hasattr(db, "tables"):
         db.loadSchema()