def _initTimeout(self, alwaysReset=1): """Initialize the signal handler needed to properly catch a SIGALRM if called while executing a query.""" #print 'setting handler' signal.signal(signal.SIGALRM, _timeoutCB) if self.swap_signals: #print 'swapping handler' if _hasSwapSignalHandlers: MySQLdb.swapSignalHandlers([signal.SIGALRM]) self.reset_handler = alwaysReset
def open(self, timeout=0, dictcursor=True): """Open connection to database. @param timeout: timeout in seconds @type timeout: int @param dictcursor: by default, all rows returned from the database are dictionaries. Setting this to False will return them as tuples. @type dictcursor: boolean """ if self.connection: raise Database.SQLConnectionError, \ "connection already established, call close() first" # open db connection try: self.dprint('open database connection') self.connection = MySQLdb.connect(host=self.dbhost, user=self.user, passwd=self.password, db=self.db, connect_timeout=timeout, autocommit=self.autocommit) except MySQLdb.MySQLError: raise Database.SQLConnectionError, \ "unable to establish connection with database %s on "\ "%s as user '%s'" % (self.db, self.dbhost, self.user) else: pass
def close(self): """Close connection with database.""" self.dprint('close database connection') # close connection try: if self.connection: self.connection.close() self.connection = None except MySQLdb.MySQLError: raise Database.SQLConnectionError, \ 'unable to close database connection with %s on %s' % \ (self.db, self.dbhost) # unswap all the signal handlers when we close if self.swap_signals: if _hasSwapSignalHandlers: MySQLdb.unswapSignalHandlers() self.reset_handler = True