Edgewall Software

Changeset 7375

Show
Ignore:
Timestamp:
07/22/2008 11:41:32 AM (7 weeks ago)
Author:
cboos
Message:

Don't keep hold of a connection into a CachedRepository instance.

Those objects are themselves cached for the duration of a request, so this can prevent a db connection to be returned to the pool during all this time.

Location:
branches/0.11-stable/trac/versioncontrol
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/0.11-stable/trac/versioncontrol/cache.py

    r7306 r7375  
    4040    has_linear_changesets = False 
    4141 
    42     def __init__(self, db, repos, authz, log): 
     42    def __init__(self, getdb, repos, authz, log): 
    4343        Repository.__init__(self, repos.name, authz, log) 
    44         self.db = db 
     44        if callable(getdb): 
     45            self.getdb = getdb 
     46        else: 
     47            self.getdb = lambda: getdb 
    4548        self.repos = repos 
    4649 
     
    5457    def get_changeset(self, rev): 
    5558        return CachedChangeset(self.repos, self.repos.normalize_rev(rev), 
    56                                self.db, self.authz) 
     59                               self.getdb, self.authz) 
    5760 
    5861    def get_changesets(self, start, stop): 
    59         cursor = self.db.cursor() 
     62        db = self.getdb() 
     63        cursor = db.cursor() 
    6064        cursor.execute("SELECT rev FROM revision " 
    6165                       "WHERE time >= %s AND time < %s " 
     
    7175    def sync_changeset(self, rev): 
    7276        cset = self.repos.get_changeset(rev) 
    73         cursor = self.db.cursor() 
     77        db = self.getdb() 
     78        cursor = db.cursor() 
    7479        cursor.execute("UPDATE revision SET time=%s, author=%s, message=%s " 
    7580                       "WHERE rev=%s", (to_timestamp(cset.date), 
    7681                                        cset.author, cset.message, 
    7782                                        (str(cset.rev)))) 
    78         self.db.commit() 
     83        db.commit() 
    7984         
    8085    def sync(self, feedback=None): 
    81         cursor = self.db.cursor() 
    82  
     86        db = self.getdb() 
     87        cursor = db.cursor() 
    8388        cursor.execute("SELECT name, value FROM system WHERE name IN (%s)" % 
    8489                       ','.join(["'%s'" % key for key in CACHE_METADATA_KEYS])) 
     
    104109                           (self.name, CACHE_REPOSITORY_DIR)) 
    105110 
    106         self.db.commit() # save metadata changes made up to now 
     111        db.commit() # save metadata changes made up to now 
    107112 
    108113        # -- retrieve the youngest revision in the repository 
     
    187192                        # notion of 'youngest' 
    188193                        self.repos.clear(youngest_rev=self.youngest) 
    189                         self.db.rollback() 
     194                        db.rollback() 
    190195                        return 
    191196 
     
    214219                    cursor.execute("UPDATE system SET value=%s WHERE name=%s", 
    215220                                   (str(self.youngest), CACHE_YOUNGEST_REV)) 
    216                     self.db.commit() 
     221                    db.commit() 
    217222 
    218223                    # 1.5. provide some feedback 
     
    250255 
    251256    def _next_prev_rev(self, direction, rev, path=''): 
     257        db = self.getdb() 
    252258        # the changeset revs are sequence of ints: 
    253259        sql = "SELECT rev FROM node_change WHERE " + \ 
    254               self.db.cast('rev', 'int') + " " + direction + " %s" 
     260              db.cast('rev', 'int') + " " + direction + " %s" 
    255261        args = [rev] 
    256262 
     
    263269            sql += " OR " 
    264270            # changes on path children 
    265             sql += "path "+self.db.like() 
    266             args.append(self.db.like_escape(path+'/') + '%') 
     271            sql += "path "+db.like() 
     272            args.append(db.like_escape(path+'/') + '%') 
    267273            sql += " OR " 
    268274            # deletion of path ancestors 
     
    274280            sql += ")" 
    275281 
    276         sql += " ORDER BY " + self.db.cast('rev', 'int') + \ 
     282        sql += " ORDER BY " + db.cast('rev', 'int') + \ 
    277283                (direction == '<' and " DESC" or "") + " LIMIT 1" 
    278284         
    279         cursor = self.db.cursor() 
     285        cursor = db.cursor() 
    280286        cursor.execute(sql, args) 
    281287        for rev, in cursor: 
     
    302308class CachedChangeset(Changeset): 
    303309 
    304     def __init__(self, repos, rev, db, authz): 
     310    def __init__(self, repos, rev, getdb, authz): 
    305311        self.repos = repos 
    306         self.db = db 
     312        self.getdb = getdb 
    307313        self.authz = authz 
    308         cursor = self.db.cursor() 
     314        db = self.getdb() 
     315        cursor = db.cursor() 
    309316        cursor.execute("SELECT time,author,message FROM revision " 
    310317                       "WHERE rev=%s", (str(rev),)) 
     
    319326 
    320327    def get_changes(self): 
    321         cursor = self.db.cursor() 
     328        db = self.getdb() 
     329        cursor = db.cursor() 
    322330        cursor.execute("SELECT path,node_type,change_type,base_path,base_rev " 
    323331                       "FROM node_change WHERE rev=%s " 
  • branches/0.11-stable/trac/versioncontrol/svn_fs.py

    r7157 r7375  
    279279            repos = fs_repos 
    280280        else: 
    281             repos = CachedRepository(self.env.get_db_cnx(), fs_repos, None, 
     281            repos = CachedRepository(self.env.get_db_cnx, fs_repos, None, 
    282282                                     self.log) 
    283283            repos.has_linear_changesets = True