def _retry_on_exceptions(exc):
if not isinstance(exc, exception.DBError):
return False
inn_e = exc.inner_exception
if not isinstance(inn_e, sqlalchemy.exc.InternalError):
return False
return ((
pymysql and
isinstance(inn_e.orig, pymysql.err.InternalError) and
(inn_e.orig.args[0] == pymysql.constants.ER.TABLE_DEF_CHANGED)
) or (
# HACK(jd) Sometimes, PostgreSQL raises an error such as "current
# transaction is aborted, commands ignored until end of transaction
# block" on its own catalog, so we need to retry, but this is not
# caught by oslo.db as a deadlock. This is likely because when we use
# Base.metadata.create_all(), sqlalchemy itself gets an error it does
# not catch or something. So this is why this function exists. To
# paperover I guess.
psycopg2
and isinstance(inn_e.orig, psycopg2.InternalError)
# current transaction is aborted
and inn_e.orig.pgcode == '25P02'
))
python类InternalError()的实例源码
def create_extension(conn, extension_name, test=False):
# The following error means that pglogical package is not installed into the operating system
# ERROR: could not open extension control file "/usr/share/postgresql/9.6/extension/pglogical.control":
# The following error means that pglogical is installed but not configured correctly
# ERROR: pglogical is not in shared_preload_libraries
cur = conn.cursor()
try:
cur.execute("CREATE EXTENSION IF NOT EXISTS %s" % extension_name)
if not test:
conn.commit()
except psycopg2.InternalError as e:
msg = str(e)
if msg.find('shared_preload_libraries'):
return 'InstalledNoSharedLibraries'
return 'NotInstalled'
except psycopg2.OperationalError:
return 'NotInstalled'
finally:
if test:
conn.rollback()
return True
def find_by_id(self, _id, attempt = 0):
with self.conn.cursor() as cur:
try:
cur.execute(
"""SELECT id FROM page WHERE url = '{}'""".format(_id)
)
return cur.fetchone()
except (psycopg2.IntegrityError, psycopg2.InternalError) as err:
if attempt < 5:
return self.find_by_id(_id, attempt + 1)
else:
raise err
def execute(self, query):
retVal = False
try:
self.cursor.execute(query)
retVal = True
except (psycopg2.OperationalError, psycopg2.ProgrammingError), msg:
logger.warn(("(remote) %s" % msg).strip())
except psycopg2.InternalError, msg:
raise SqlmapConnectionException(msg)
self.connector.commit()
return retVal
def insert_sysparam(self,key,value):
cursor=self.conn.cursor()
for attempt in range(1,10):
try:
cursor.execute("insert into system (key,value,pid) values (%s,%s,%s)", (key, value, os.getpid()))
break
except psycopg2.InternalError: #if we have an aborted transaction, cleanup and then re-try
self.conn.rollback()
continue
def execute(self, query):
retVal = False
try:
self.cursor.execute(query)
retVal = True
except (psycopg2.OperationalError, psycopg2.ProgrammingError), msg:
logger.warn(("(remote) %s" % msg).strip())
except psycopg2.InternalError, msg:
raise SqlmapConnectionException(msg)
self.connector.commit()
return retVal
def execute(self, query):
retVal = False
try:
self.cursor.execute(query)
retVal = True
except (psycopg2.OperationalError, psycopg2.ProgrammingError), msg:
logger.warn(("(remote) %s" % msg).strip())
except psycopg2.InternalError, msg:
raise SqlmapConnectionException(msg)
self.connector.commit()
return retVal
def execute(self, query):
retVal = False
try:
self.cursor.execute(query)
retVal = True
except (psycopg2.OperationalError, psycopg2.ProgrammingError), msg:
logger.warn(("(remote) %s" % msg).strip())
except psycopg2.InternalError, msg:
raise SqlmapConnectionException(msg)
self.connector.commit()
return retVal
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
def _query_worker(self, cmd, dict):
try:
self.cur.execute(cmd, dict)
result = self.cur.fetchall()
self.conn.commit()
except psycopg2.ProgrammingError as ex:
if ex.message == 'no results to fetch':
result = None
elif ex.message == 'can\'t execute an empty query':
result = None
else:
raise ex
except psycopg2.InternalError as uiex:
text = uiex.message
text += u'\nSQL Query: {}'.format(cmd)
text += u'\nSQL Parameter Dict: {}'.format(dict)
result = None
self._auto_gen.rec_error(text, traceback.format_exc().decode('utf-8'), u'(SQL DB)')
self.close_connection()
self.set_connection()
self.sql_cmd(cmd, dict)
except Exception as e:
text = e.message
text += u'\nSQL Query: {}'.format(cmd)
text += u'\nSQL Parameter Dict: {}'.format(dict)
result = None
self._auto_gen.rec_error(text, traceback.format_exc().decode('utf-8'), u'(SQL DB)')
raise e
if result is not None:
if len(result) > 0:
return result
else:
return None
else:
return None
def get_pkey(table, conn=None, **kwargs):
'''
Return the primary key column for a table as a named tuple with fields
"column" and "type"
If no primary key, return None
Ref: https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
'''
p_key = namedtuple('PrimaryKey', ['column', 'type'])
cur = conn.cursor()
try:
cur.execute(sql.SQL('''
SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type
FROM pg_index i
JOIN pg_attribute a ON a.attrelid = i.indrelid
AND a.attnum = ANY(i.indkey)
WHERE i.indrelid = {}::regclass
AND i.indisprimary;
''').format(
sql.Literal(table)))
data = cur.fetchall()[0]
return p_key(column=data[0], type=data[1])
except IndexError:
return None
except (psycopg2.ProgrammingError, psycopg2.InternalError) as e:
conn.rollback()
return None