处理psycopg2.InternalError'错误:当前事务被中止,命令被忽略直到事务块结束'

时间:2018-03-21 03:34:19

标签: python postgresql psycopg2

我遇到了标题所示的问题:

InternalError: current transaction is aborted, commands ignored until end of transaction block

我找到了3个解决方案:

  1. with声明;
  2. rollback();
  3. autocommit属性; (似乎太全球化了)
  4. 我已经尝试了with声明,但不适合我的情况。在这里,我需要cursor供以后使用,我建立了像Django这样的get()方法,因此无法关闭。

    好吧,我想我可以抓住某个错误来执行rollback(),然后转到execute()。由于InternalError是一个混合异常,我会根据pgerror属性捕获错误。

    class PostgresqlConnector(object):
    
        def __init__(self, config):
            self.conn = psycopg2.connect(**config)
            self.cursor = self.conn.cursor()
    
            # Add exceptions
            self.DoesNotExist = DoesNotExist
            self.MultipleObjectsReturned = MultipleObjectsReturned
    
        def __commit__(self):
            self.conn.commit()
    
        def __rollback__(self):
            self.conn.rollback()
    
        def __execute__(self, sql, vals):
            try:
                self.cursor.execute(sql, vals)
            except psycopg2.InternalError as e:
                if e.pgerror == 'ERROR:  current transaction is aborted, commands ignored until end of transaction block\n':
                    self.__rollback__()
                    return self.__execute__(sql, vals)
                raise e
            else:
                self.__commit__()
                return self.cursor
    
        def select(self, tab, query_dict, cols_selected='*', filter_='='):
            cols, vals = zip(*query_dict.items())
            cols_selected = ', '.join(cols_selected)
            expr_where = ' AND '.join(["{} {} %s".format(x, filter_) for x in cols])
    
            sql = "SELECT {} FROM \"{}\" WHERE {}".format(cols_selected, tab, expr_where)
            return self.__execute__(sql, vals)
    
        # --- omit ---
    
        def get(self, tab, query_dict, cols_selected='*', filter_='='):
            # here i need cursor
            self.cursor = self.select(tab, query_dict, cols_selected, filter_)
            if self.cursor.rowcount == 1:
                return self.cursor.fetchone()
            elif self.cursor.rowcount == 0:
                raise self.DoesNotExist("No record found!")
            else:
                raise self.MultipleObjectsReturned("Get {} records! only one excepted.".format(self.cursor.rowcount))
    
        def close(self):
            self.cursor.close()
            self.conn.close()
    

    我的问题是我做对了吗?有更好的解决方案吗?

0 个答案:

没有答案