Example #1
0
    def cursor(self):
        """
        Returns a database cursor, automatically re-opening the database
        connection if necessary.

        """
        try:
            try:
                cursor = self.db.cursor()
                cursor.execute('SELECT 1')
            except psycopg2.InternalError as err:
                if err.pgcode == IN_FAILED_SQL_TRANSACTION:
                    LOGGER.critical("Rolling back aborted transaction...")
                    self.db.rollback()
                else:
                    LOGGER.critical(
                        "PostgreSQL reported an internal error "
                        "I don't know how to handle: %s "
                        "(code=%s)", pg_err_lookup(err.pgcode), err.pgcode)
                    raise
        except Exception as err:
            if self.db is not None:
                LOGGER.critical("Could not get cursor. Trying to reconnect...",
                                exc_info=True)
            self.close()
            self.connect()
            cursor = self.db.cursor()
        return cursor
Example #2
0
 def cursor(self):
     try:
         try:
             cursor = self.db.cursor()
             cursor.execute('SELECT 1')
         except psycopg2.InternalError, err:
             if err.pgcode == IN_FAILED_SQL_TRANSACTION:
                 debug("Rolling back aborted transaction...", 2)
                 self.db.rollback()
             else:
                 debug("PostgreSQL reported an internal error I don't know "
                       "how to handle: %s (code=%s)" % (
                         pg_err_lookup(err.pgcode), err.pgcode), 2)
                 raise
     except Exception, err:
         debug(str(err), 2)
         debug("Could not get cursor. Trying to reconnect...", 2)
         self.close()
         self.connect()
         cursor = self.db.cursor()