Changeset 1379


Ignore:
Timestamp:
02/22/07 09:22:46 (6 years ago)
Author:
mglb1
Message:
  • Move database connection establishment into a separate function.
  • Reconnect whenever 'serious' database connection errors occur
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ccsd/trunk/crcnetd/_utils/ccsd_session.py

    r1358 r1379  
    7878        self.mode = mode 
    7979        self.lock = threading.RLock() 
    80          
     80        self.db = None 
     81 
    8182        # Session Time Records 
    8283        if initiated == -1: 
     
    9596            self.token = token 
    9697         
    97         # Setup a database connection   
    98         self.db = PgSQL.connect(host=self.dhost, database=self.database, \ 
    99                 user=self.duser, password=self.dpass) 
     98        # Setup a database connection 
     99        self._connect() 
    100100         
    101101        # Setup session in the database 
     
    133133                (self.session_id, self.username, self.mode)) 
    134134     
     135    def _connect(self): 
     136        # Close the existing connection 
     137        if self.db is not None: 
     138            try: 
     139                self.db.close() 
     140            except: 
     141                log_warn("Could not close existing db connection cleanly " \ 
     142                        "in session #%s" % self.session_id, sys.exc_info()) 
     143 
     144        # Setup a database connection   
     145        self.db = PgSQL.connect(host=self.dhost, database=self.database, \ 
     146                user=self.duser, password=self.dpass) 
     147 
    135148    def getSessionObject(self): 
    136149        """Returns a dictionary containing information about the session""" 
     
    351364        except: 
    352365            log_error("Unable to obtain cursor for query!", sys.exc_info()) 
     366            # This pretty much means our db connection is hosed, reconnect 
     367            self._connect() 
    353368            self.lock.release() 
    354369            raise ccsd_session_error("Unable to obtain cursor for query!") 
     
    448463            commit = 1 
    449464         
     465        # Obtain a cursor 
     466        try: 
     467            cursor = self.db.cursor() 
     468        except: 
     469            # This pretty much means our db connection is hosed, reconnect 
     470            self._connect() 
     471            log_error("Could not obtain cursor for execute", sys.exc_info()) 
     472            self.lock.release() 
     473            raise ccsd_session_error("Could not obtain cursor") 
     474             
    450475        # Execute the query 
    451476        try: 
    452             cursor = self.db.cursor() 
    453477            res = cursor.execute(sql, params) 
    454478        except: 
     
    634658 
    635659        # Setup a database connection   
    636         self.db = PgSQL.connect(host=self.dhost, database=self.database, \ 
    637                 user=self.duser, password=self.dpass) 
     660        self._connect() 
    638661         
    639662        # Steal a session ID from the sequence, but don't put the session  
Note: See TracChangeset for help on using the changeset viewer.